Enum idiom

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(See also)
 
(One intermediate revision by one user not shown)
Line 89: Line 89:
 
* [[Replace Type Code with Class]]
 
* [[Replace Type Code with Class]]
 
* [[Beware value switches]]
 
* [[Beware value switches]]
* [[Cay Horstmann 2002]].
+
* [[Cay Horstmann 2002]]
* [[http://www.cosc.canterbury.ac.nz/resources/software/java/books/TIJ-3.4.0/html/TIJ310.htm#Heading7029 Bruce Eckel's example]]
+
* [http://www.cosc.canterbury.ac.nz/resources/software/java/books/TIJ-3.4.0/html/TIJ310.htm#Heading7029 Bruce Eckel's example]
 
* http://java.sun.com/developer/JDCTechTips/2001/tt0807.html#tip2
 
* http://java.sun.com/developer/JDCTechTips/2001/tt0807.html#tip2
 
* http://jcp.org/aboutJava/communityprocess/jsr/tiger/enum.html
 
* http://jcp.org/aboutJava/communityprocess/jsr/tiger/enum.html

Latest revision as of 02:03, 3 August 2009

Prior to version 1.5, Java had no native support for enumerated types. This led to some nasty practices, and an idiom to avoid the nastiness. In Java 1.5, the idiom has become part of the language.

Contents

Old Java enum idiom

Cay Horstmann 2002, p269 gives an idiom for implementing enums in Java.

Common practice

   public static final int SMALL  = 1;
   public static final int MEDIUM = 2;
   public static final int LARGE  = 3;

But this is not very satisfactory, because the compiler can't check type errors. E.g.

   int size = LARGE;
   size++;

The typesafe enumeration idiom

Make a class to define the enum.

   public class Size {
       private String name;
       public static final Size SMALL  = new Size("SMALL");
       public static final Size MEDIUM = new Size("MEDIUM");
       public static final Size LARGE  = new Size("LARGE");
       private Size(String name) {
           this.name = name;
       }
       public String toString() {
           return name;
       }
   }

Notes:

  • You don't really need the 'name' String. It is useful for debugging, though.
  • You can add whatever other attributes you like to the class.
  • The constructor is private, so illlegal values can't be created.

The Java 1.5 solution

Java 1.5 has new syntax to support this idiom.

If you have simple needs, it looks like this:

   public enum Size { SMALL, MEDIUM, LARGE }

You can use `Size` like this:

   Size howBig = Size.MEDIUM;


And, (if you know the new Java `for` syntax), like this:

   for (Size s: Size.values())
       System.out.println(s);


Under the hood, Java is really creating a new class called `Size`, just like the idiom. This means you can do fancier things, like:

   public enum Size {
       SMALL(10),
       MEDIUM(14),
       LARGE(18);
       private int diameter;
       public Size(int theDiameter) {
           diameter = theDiameter;
       }
       public void printDiameter() {
           System.out.println(diameter);
       }
   }

As you would expect, we can use our enum like this:

   for (Size s: Size.values())
       s.printDiameter();


See also

Personal tools