Declaring Enum Constructors and Members
An enum type can declare constructors and other members as in an ordinary class, but the enum constants must be declared before any other declarations (see the declaration of the enum type Meal in Example 5.25). The list of enum constants must be terminated by a semicolon (;) if followed by any constructor or member declaration. Each enum constant name can be followed by an argument list that is passed to the constructor of the enum type having the matching parameter signature.
In Example 5.25, the enum type Meal contains a constructor declaration at (2) with the following signature:
Meal(int, int)
Each enum constant is specified with an argument list with the signature (int, int) that matches the non-zero argument constructor signature. The enum constant list is also terminated by a semicolon, as the enum declaration contains other members: two fields for the meal time at (3), and three instance methods to retrieve meal time information at (4).
When the enum type is loaded at runtime, the constructor is run for each enum constant, passing the argument values specified for the enum constant. For the Meal enum type, three objects are created that are initialized with the specified argument values, and are referenced by the three enum constant names, respectively. Note that each enum constant is a final, static reference that stores the reference value of an object of the enum type, and methods of the enum type can be called on this object by using the enum constant name. This is illustrated at (5) in Example 5.25 by calling methods on the object referenced by the enum constant Meal.BREAKFAST.
A default constructor is created if no constructors are provided for the enum type, analogous to a class. As mentioned earlier, an enum type cannot be instantiated using the new operator. The constructors cannot be called explicitly. Thus the only access modifier allowed for a constructor is private, as a constructor is understood to be implicitly declared private if no access modifier is specified.
Static initializer blocks can also be declared in an enum type, analogous to those in a class (§10.7, p. 545).
Example 5.25 Declaring Enum Constructors and Members
// File: Meal.java
public enum Meal {
BREAKFAST(7,30), LUNCH(12,15), DINNER(19,45); // (1)
// Non-zero argument constructor (2)
Meal(int hh, int mm) {
this.hh = hh;
this.mm = mm;
}
// Fields for the meal time: (3)
private int hh;
private int mm;
// Instance methods: (4)
public int getHour() { return this.hh; }
public int getMins() { return this.mm; }
public String getTimeString() { // “hh:mm”
return String.format(“%02d:%02d”, this.hh, this.mm);
}
}
// File: MealAdministrator.java
public class MealAdministrator {
public static void main(String[] args) {
System.out.printf( // (5)
“Please note that no eggs will be served at %s, %s.%n”,
Meal.BREAKFAST, Meal.BREAKFAST.getTimeString()
);
System.out.println(“Meal times are as follows:”);
Meal[] meals = Meal.values(); // (6)
for (Meal meal : meals) { // (7)
System.out.printf(“%s served at %s%n”, meal, meal.getTimeString());
}
Meal formalDinner = Meal.valueOf(“DINNER”); // (8)
System.out.printf(“Formal dress is required for %s at %s.%n”,
formalDinner, formalDinner.getTimeString()
);
}
}
Output from the program:
Please note that no eggs will be served at BREAKFAST, 07:30.
Meal times are as follows:
BREAKFAST served at 07:30
LUNCH served at 12:15
DINNER served at 19:45
Formal dress is required for DINNER at 19:45.
Leave a Reply