1

Given

public enum Title {

MR("Mr."), MRS("Mrs."), MS("Ms.");

private final String title;

private Title(String t) {
    title = t;
}

public String format(String last, String first) {
    return title + "" + first + "" + last;
}
}

public static void main(String[] args){
    System.out.println(Title.MR.format("Doe","John"));
}

Does anyone know how to explain this ? Keep in mind the code is not fully complete. This happens to be a question from a book. The Answer to this question is Mr. John Doe.

Thanks

3
  • 8
    What part do you not understand? Commented Jul 10, 2010 at 12:43
  • What does the book say? This is really simple code ...
    – Pointy
    Commented Jul 10, 2010 at 12:48
  • 4
    Actually, the answer would be Mr.JohnDoe Commented Jul 10, 2010 at 13:13

3 Answers 3

3

Well, first of all, consider reading this, in order to understand what is an Enum, how it works, and when you should use it.

Now, concerning your Enum example, you are declaring an Enumerated type with three possible values: MR, MRS, and MS. Enums, just like Classes, can have methods and constructors. In your example, Title has a single argument constructor -- which stores a description of the Title --, and a method that basically prepends the description to a given name -- the format method.

So, when you invoke Title.MR.format("Doe","John")), first you get an instance of the MR Title, and then you invoke the format method, which returns Mr.John Doe.

Also note that only one instance of each Title is created, so that invoking Title.MR several times will always return the same objet.

0
3

This is a rewriting of the original snippet from an enum to a class; it captures most what the original snippet does. I also rewrote the string concatenation part using String.format for instructional purposes.

public class Title {

    public static final Title MR = new Title("Mr.");
    public static final Title MRS = new Title("Mrs.");
    public static final Title MS = new Title("Ms.");

    private final String title;
    private Title(String t) { title = t; }

    public String format(String last, String first) {
        return String.format("%s %s %s", title, first, last);
    }

    public static void main(String[] args){
        System.out.println(Title.MR.format("Doe","John"));
        // prints "Mr. John Doe"
    }
}

Note the similarity with the enum code. But note also that this version is much more verbose. It's important that you know enough Java to understand why this version works the way it is. Once you do, understanding the enum version is simple: enum are classes in Java. It is not as "simple" a type as the C/C++ counterpart.

References

Related questions

On Java vs C++ difference:

On various enum usage:

See also

  • Effective Java 2nd Edition, Chapter 6: Enums and Annotations
    • Item 30: Use enums instead of int constants
    • Item 31: Use instance fields instead of ordinals
    • Item 32: Use EnumSet instead of bit fields
    • Item 33: Use EnumMap instead of ordinal indexing

API links

1
  • Very descriptive. Thanks for your time.
    – Haxed
    Commented Jul 11, 2010 at 4:22
2

Supposing the question is how the result is fabricated, here's the source code with some comments:

public enum Title { // Declare an enum named Title

// Declare the enumerations with parameters that correspond with the constructor
MR("Mr."), MRS("Mrs."), MS("Ms.");

private final String title; // Declare String attribute (has null value at this point)

private Title(String t) { // enum constructor which accepts a String
    title = t; // Initialize title String with parameter t
}
// format method which accepts last and first Strings and returns a String
public String format(String last, String first) {
    // return is a String constructed from title first and last Strings.
    return title + "" + first + "" + last;
}
}

// Main method which is triggered when the class is run
public static void main(String[] args){
    // Output to console a call to the MR enum of Title with parameters Doe and John
    System.out.println(Title.MR.format("Doe","John"));
}

Hope that makes the code clearer.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.