Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a rewriting of the original snippet from an <code>enum</code> to a <code>class</code>; it captures <em>most</em> what the original snippet does. I also rewrote the string concatenation part using <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29" rel="nofollow noreferrer"><code>String.format</code></a> for instructional purposes.</p> <pre><code>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" } } </code></pre> <p>Note the similarity with the <code>enum</code> 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 <code>enum</code> version is simple: <code>enum</code> are classes in Java. It is not as "simple" a type as the C/C++ counterpart.</p> <h3>References</h3> <ul> <li><a href="http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/guide/language/enums.html" rel="nofollow noreferrer">Java Language Guide/Enums</a></li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9" rel="nofollow noreferrer">JLS 8.9 Enums</a></li> </ul> <h3>Related questions</h3> <p>On Java vs C++ difference:</p> <ul> <li><a href="https://stackoverflow.com/questions/2080681/difference-of-enum-between-java-and-c">Difference of Enum between java and C++?</a></li> <li><a href="https://stackoverflow.com/questions/2077634/enum-in-java-advantages">Enum in Java. Advantages?</a></li> </ul> <p>On various <code>enum</code> usage:</p> <ul> <li><a href="https://stackoverflow.com/questions/3054247/java-enum-item-description">Java Enum Item Description</a></li> <li><a href="https://stackoverflow.com/questions/2780129/get-enum-by-its-inner-field">Get enum by its inner field</a></li> <li><a href="https://stackoverflow.com/questions/2457076/can-i-add-a-function-to-enums-in-java">Can I add a function to enums in Java?</a> (<em>ABSOLUTELY!</em>)</li> <li><a href="https://stackoverflow.com/questions/2765687/is-there-a-a-c-like-way-to-get-item-number-from-enum-in-java">Is there a a C-like way to get item number from enum in java ?</a> <ul> <li>(Yes, but why when there are so much better alternatives!?</li> </ul></li> </ul> <h3>See also</h3> <ul> <li><em>Effective Java 2nd Edition, Chapter 6: Enums and Annotations</em> <ul> <li><em>Item 30: Use enums instead of <code>int</code> constants</em></li> <li><em>Item 31: Use instance fields instead of ordinals</em></li> <li><em>Item 32: Use <code>EnumSet</code> instead of bit fields</em></li> <li><em>Item 33: Use <code>EnumMap</code> instead of ordinal indexing</em></li> </ul></li> </ul> <h3>API links</h3> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/lang/Enum.html" rel="nofollow noreferrer"><code>java.lang.Enum</code></a></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/lang/EnumSet.html" rel="nofollow noreferrer"><code>java.lang.EnumSet</code></a></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/EnumMap.html" rel="nofollow noreferrer"><code>java.util.EnumMap</code></a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload