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
A set of "enumerable constants" is an ordered collection of constants that can be counted, like numbers. That property lets you use them like numbers to index an array, or you can use them as the index variable in a for loop. In Java, such objects are most often known as "enumerated constants."

Using enumerated constants can make code more readable. For example, you might want to define a new data type named Color with constants RED, GREEN, and BLUE as its possible values. The idea is to have Color as an attribute of other objects you create, such as Car objects:

  class Car {
     Color color;
     ...
  }


Then you can write clear, readable code, like this:

  myCar.color = RED;


instead of something like:

  myCar.color = 3;


An even more important attribute of enumerated constants in languages like Pascal is that they are type safe. In other words, it is not possible to assign an invalid color to the color attribute -- it must always be either RED, GREEN, or BLUE. In contrast, if the color variable were an int, then you could assign any valid integer to it, even if that number did not represent a valid color.

This article gives you a template for creating enumerated constants that are:

  • Type safe
  • Printable
  • Ordered, for use as an index
  • Linked, for looping forwards or backwards
  • Enumerable


In a future article, you will learn how to extend enumerated constants to implement state-dependent behavior.

Why not use static finals?

A common mechanism for enumerated constants uses static final int variables, like this:

  static final int RED = 0;
  static final int GREEN = 1;
  static final int BLUE = 2;
  ...


Static finals are useful
Because they are final, the values are constant and unchangeable. Because they are static, they are only created once for the class or interface in which they are defined, instead of once for every object. And because they are integer variables, they can be enumerated and used as an index.

For example, you can write a loop to create a list of a customer's favorite colors:

  for (int i=0; ...) {
    if (customerLikesColor(i)) {
       favoriteColors.add(i);
    }
  }


You can also index into an array or a vector using the variables to get a value associated with the color. For example, suppose you have a board game that has different colored pieces for each player. Let's say you have a bitmap for each color piece and a method called display() that copies that bitmap to the current location. One way to put a piece on the board might be something like this:

  PiecePicture redPiece = new PiecePicture(RED);
  PiecePicture greenPiece = new PiecePicture(GREEN);
  PiecePicture bluePiece = new PiecePicture(BLUE);

void placePiece(int location, int color) { setPosition(location); if (color == RED) { display(redPiece); } else if (color == GREEN) { display(greenPiece); } else { display(bluePiece); } }


But by using the integer values to index into an array of pieces, you can simplify the code to:

  PiecePicture[] piece = {new PiecePicture(RED), 
                          new PiecePicture(GREEN), 
                          new PiecePicture(BLUE) 
                         };
  void placePiece(int location, int color) {
    setPosition(location);
    display(piece[color]);
  }


Being able to loop over a range of constants and index into an array or vector are the major advantages of static final integers. And when the number of choices grows, the simplification effect is even greater.

  • Print
  • Feedback

Resources