“enum (short for “enumeration”) is a data special data type that allows a variable to be a set of predefined constants.” — The Java Tutorials
An enum is the platypus of Java artifacts. It behaves like a class in that it defines the format of a set of instances. It has values, but the values behave in some ways like primitives and in some ways like objects. They can’t be instantiated, any more than you can instantiate the number 42; but they can have methods and fields, and the fields can be mutable.
Specifically, an enum class (we’ll use “enum class” to refer to the whole enum declaration, and “enum” to refer to one of its values) is a list of constant names which can then be assigned to variables of the enum class. To further quote Oracle, “Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.” Since enums are constants, they are by convention written in all caps with words separated by underscores.
So here is an example of defining and using an enum class.
// The next three lines are the enum class definition.
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
}
Suit mySuit = Suit.DIAMONDS;
if (mySuit == Suit.CLUBS {...}
System.out.println(mySuit);
(An enum can be defined inside a class definition if you need it only locally, or it can appear in its own source file if multiple classes need it.)
The first (and in this case, only) thing in the enum class declaration is the list of enum names separated by commas. If there’s nothing more to the class, a terminating semicolon is optional.
The last line in the snippet above would print the name of the particular enum: CLUBS, DIAMONDS, etc.
Notice how the == operator is used to compare the values of enums (and how the enum value needed to be qualified here.) == and equals()
are equivalent for enums, since an enum instance is not an object instance; it’s a value. In fact, in an enum, the equals()
method can’t be overridden.
But Wait–There’s More!
enums aren’t limited to the names of their various values. They can have fields and methods as well (both instance and static), including constructors.
And they’re not immutable. In my experience, I had an enum consisting of message code numbers and texts, but users were free to establish substitute message texts for each code value. The text was read from a database, and the text value within the appropriate enum instance was set accordingly via a setter method.
So how about a Rank enum which has a constructor in which we pass the relative value of each card:
public enum Rank {
DEUCE(2), TREY(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7),
EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12),
KING(13), ACE(14);
private int value;
private Rank(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
As before, the names come first. But this enum class also has a constructor, and each name is followed by an argument list to pass to that constructor. (Since there’s more to the enum class definition than the names, the semicolon is required.) And the enum class also has a field and a getter.
enum Methods
We’ve already mentioned that you can define your own methods, but all enums come with these:
Method | Function |
int compareTo(Enum other) | An instance method: Returns a negative int if the enum has a lower ordinal value, 0 if an equal ordinal value, and a positive int if a greater value, than that of other. other must be an enum of the same type. Ordinal value is the order in which the enum names are declared. In our example, SPADES has the largest value, and CLUBS the lowest. compareTo() for enums is a final method–you can’t override it. |
String name() | Returns the declared name of the enum constant. (As with other artifacts, enum itself as a String returns the result of the toString() method, which can be overridden to return a more user-friendly result.) name() can be used to return a value that absolutely matches the declaration. |
int ordinal() | Returns the ordinal position of the enum among its declarations, starting with 0. In our example, CLUBS.ordinal() == 0 and SPADES.ordinal() == 3. Use of the ordinal() method is discouraged in favor of the use of name() and compareTo() as appropriate. |
static T valueOf(String name) | Returns the enum declared with the name passed in s. Example: Suit.valueOf("HEARTS") |
static T[] values() | Returns an array of the declared enums. You can iterate over this array to process all the enums. Example: for (Suit suit : Suit.values()) { System.out.println( card.suit); } |
Notice that we didn’t have to add a value field to the Rank enum class in order to compare the ranks of two cards; as long as they’re declared in order from lowest to highest, compareTo() can determine which is greater.
What You Need To Know
- An enum (which we’re calling an enum class for clarity) is an ordered set of named items (which we’re calling enums) which, optionally, may have methods and/or mutable fields.
- An enum can be assigned to a variable of its enum class type.
- Given the appropriate context, your IDE’s code completion facility can help select from an enum class’s permissible values when making references to them.
Next: Controlling the Flow