Java enum

February 9th, 2009

One of the cooler features introduced in JDK 1.5 was the enum keyword.

In short, an enum is a collection of constant values. They provide a simple way of limiting the options you can use for a variable if there are only a standard, or in other words enumerated, set of values it can be.

For instance, imagine if you were modeling a deck of cards. Each card would be one of 4 possible suits: clubs, diamonds, spades, or hearts. One option is to store the suit in a string variable, however this is cumbersome for two reasons:

  • There has to be careful validation in the setter for the suit to ensure only the four possible suits are allowed, taking into account the capitalization of the string.
  • If you needed to do different handling based on the suit, if/elses would have to be used; a switch statement cannot be used on strings.

Instead of using a string to track the suit, an enum is a good candidate. An enum is defined just like a class except using the enum keyword instead of class. Also like a class, an enum is typically defined in its own file of the same name.

1
2
3
public enum Suit {
    HEARTS, CLUBS, SPADES, DIAMONDS
}

A simple enum, which is usually sufficient, just lists the possible values. Since these values are effectively constant, the convention is the same as for final variables (all caps).

Using an enum is the same as any other object:

1
2
3
4
5
6
7
8
9
10
11
public class Card {
    private Suit suit;
 
    public Suit getSuit() {
        return suit;
    }
 
    public void setSuit(Suit s) {
        suit = s;
    }
}

When attempting to set a suit on a card, the compiler will only allow one of the four values defined in the enum, so there is no need to add validation in the setter. The value is accessed by using the enum name and the value name, similar to calling a static method or variable:

1
2
Card card = new Card();
card.setSuit(Suit.DIAMONDS);

If for some reason, we needed to do different handling based on suit (not an overly applicable scenario in this use case, but often a common usage of enums), enums are supported in switch statements:

1
2
3
4
5
6
7
8
switch (myVariable) {
    case Suit.HEARTS:
        // do hearts processing
        break;
    case Suit.CLUBS:
        // do clubs processing
        break;
}

Keep in mind that an enum can also take on other properties of a class. Instance variables and methods can still be defined in the enum. Since the only creation of enum values are done inside of the enum itself, any instance variables must be initialized in the constructor. For example, assume we wanted a display name for each suit. The following changes would support this addition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public enum Suit {
    HEARTS("Hearts"),
    DIAMONDS("Diamonds"),
    CLUBS("Clubs),
    SPADES("Spades");
 
    private String name;
 
    private Suit(String n) {
        name = n;
    }
 
    public String getName() {
        return name;
    }
}

Given the above changes, the public methods can be called on the enum values as if they were any other object::

1
System.out.println(Suit.HEARTS.getName());

For another example, check out the enum for each column in the grades table in CodeTurtle.

Comments are closed.