views:

124

answers:

4

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

+2  A: 

What is the question ?

I saw that it is the third question you post in less than 5 hours and all are really not understandable or shows that you haven't read the Java API before to post your question.

If you have no knownledge of Java you can learn basics by following this link: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/index.html

Before to post questions related on Java API please look at the Javadoc available here: http://java.sun.com/javase/6/docs/api/

Laurent
ok i will do that first thks
Haxed
+2  A: 

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.

JG
Correct answer thks
Haxed
+2  A: 

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.

James P.
yeah was really helpful
Haxed
+2  A: 

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

polygenelubricants
Very descriptive. Thanks for your time.
Haxed