5.13 Enum Types
An enum type is a special-purpose class that defines a finite set of symbolic names and their values. These symbolic names are usually called enum constants or named constants. An enum type is also synonymously referred to as an enum class.
Before the introduction of enum types in the Java programming language, such constants were typically declared as final, static variables in a class (or an interface) declaration:
public class MachineState {
public static final int BUSY = 1;
public static final int IDLE = 0;
public static final int BLOCKED = -1;
}
Such constants are not type-safe, as any int value can be used where we need to use a constant declared in the MachineState class. Such a constant must be qualified by the class (or interface) name, unless the class is extended (or the interface is implemented). When such a constant is printed, only its value (e.g., 0), and not its name (e.g., IDLE) is printed. A constant also needs recompiling if its value is changed, as the values of such constants are compiled into the client code.
An enum type in Java is a special kind of class that is much more powerful than the approach outlined above for defining named constants.
Declaring Type-Safe Enums
The canonical form of declaring an enum type is shown below.
access_modifier
enum enum_type_name
// Enum header
// Enum body
EC1, EC2, …, ECk
// Enum constants
}
The following declaration is an example of an enum type:
public enum MachineState // Enum header
{ // Enum body
BUSY, IDLE, BLOCKED // Enum constants
}
The keyword enum is used to declare an enum type, as opposed to the keyword class for a class declaration. The basic notation requires the enum type name in the enum header, and a comma-separated list of enum constants (EC1, EC2, …, ECk) can be specified in the enum body. In the example enum declaration, the name of the enum type is MachineState. It defines three enum constants with explicit names: BUSY, IDLE, and BLOCKED. An enum constant can be any legal Java identifier, but the convention is to use uppercase letters in the name.
Essentially, an enum declaration defines a reference type that has a finite number of permissible values referenced by the enum constants, and the compiler ensures they are used in a type-safe manner.
Analogous to a top-level class, a top-level enum type can be declared with either public or package accessibility. However, an enum type declared as a static member of a reference type can be declared with any accessibility.
As we shall see later, other member declarations can be specified in the body of an enum type. If this is the case, the enum constant list must be terminated by a semicolon (;). Analogous to a class declaration, an enum type is compiled to Java byte-code that is placed in a separate class file.
The Java SE Platform API contains numerous enum types. We mention two enum types here: java.time.Month and java.time.DayOfWeek. As we would expect, the Month enum type represents the months from JANUARY to DECEMBER, and the DayOfWeek enum type represents the days of the week from MONDAY to SUNDAY. Examples of their usage can be found in §17.2, p. 1027.
Some additional examples of enum types are given below.
public enum MarchingOrders { LEFT, RIGHT }
public enum TrafficLightState { RED, YELLOW, GREEN; }
enum MealType { BREAKFAST, LUNCH, DINNER }
Leave a Reply