Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Create enumerated constants in Java

The weaknesses of Java's static finals are defined here and a template is provided for creating typesafe constants

  • Print
  • Feedback

Page 6 of 6

When an inner class references a local variable from the containing class, that variable must be declared as final. That restriction is imposed by Java so that the virtual machine and compiler do not have to worry about a variable that could be simulatenously modified in two different classes. In this case, the only such variable accessed is first, which just happens to be final.

Conclusion

In this article, you have learned how to create typesafe constants that are ordered, enumerable, printable, usable as an index, and usable in a loop. Below is the complete template for constants that meet these criteria. It's a fairly hefty chunk of code, but it is a useful template: If you do a search and replace on "Color" you can create any class of constants you like. Then you need only modify the last few lines to define the constants you need. Copy this template and modify the parts that are highlighted:

  package enumTest;
  import java.util.*;

public final class Color { private String id; public final int ord; private Color prev; private Color next;
private static int upperBound = 0; private static Color first = null; private static Color last = null; private Color(String anID) { this.id = anID; this.ord = upperBound++; if (first == null) first = this; if (last != null) { this.prev = last; last.next = this; } last = this; } public static Enumeration elements() { return new Enumeration() { private Color curr = first; public boolean hasMoreElements() { return curr != null; } public Object nextElement() { Color c = curr; curr = curr.next(); return c; } }; } public String toString() {return this.id; } public static int size() { return upperBound; } public static Color first() { return first; } public static Color last() { return last; } public Color prev() { return this.prev; } public Color next() { return this.next; }
public static final Color RED = new Color("Red"); public static final Color GREEN = new Color("Green"); public static final Color BLUE = new Color("Blue"); }


Finally, here is a test file that shows what happens when you enumerate over the constants defined in the Color class:

  package enumTest;
  import java.util.*;

public class EnumTest { //Main method static public void main(String[] args) { Enumeration e = Color.elements(); while (e.hasMoreElements()) { Color c = (Color) e.nextElement(); System.out.println(c + ": " + c.ord); } } }


Note: If compilation produces the error, "class Color not found," make sure your classpath variable includes "..". With the Color class in the enumTest package, the compiler sees "Color" as "enumTest.Color." The installation instructions for JDK 1.1 say to include "." in the classpath, but under Windows, at least, that causes the compiler to look for Color.java under the current directory (enumTest) instead of in that directory.

About the author

Eric Armstrong has been programming and writing professionally since before there were personal computers. His production experience includes AI programs, system libraries, real-time programs, and business applications in a variety of languages. He is currently writing a book on a soon-to-be-released Java IDE.
  • Print
  • Feedback

Resources