Implicit Static Methods for Enum Types
All enum types implicitly have the following static methods, and methods with these names cannot be declared in an enum type declaration:
static
EnumTypeName
[] values()
Returns an array containing the enum constants of this enum type, in the order they are specified.
static
EnumTypeName
valueOf(String name)
Returns the enum constant with the specified name. An IllegalArgumentException is thrown if the specified name does not match the name of an enum constant. The specified name is not qualified with the enum type name.
The static method values() is called at (6) in Example 5.25 to create an array of enum constants. This array is traversed in the for(:) loop at (7), printing the information about each meal.
The static method valueOf() is called at (8) in Example 5.25 to retrieve the enum constant that has the specified name “DINNER”. A print statement is used to print the information about the meal denoted by this enum constant.
Inherited Methods from the java.lang.Enum Class
All enum types are subtypes of the java.lang.Enum class which implements the default behavior. All enum types are comparable (§14.4, p. 761) and serializable (§20.5, p. 1261).
All enum types inherit the following selected final methods from the java.lang.Enum class, and these methods therefore cannot be overridden by an enum type:
final int compareTo(E o)
The natural order of the enum constants in an enum type is according to their ordinal values (see the ordinal() method below). The compareTo() method in the Comparable interface is discussed in §14.4, p. 761.
final boolean equals(Object other)
Returns true if the specified object is equal to this enum constant (§14.2, p. 744).
final int hashCode()
Returns a hash code for this enum constant (§14.3, p. 753).
final String name()
Returns the name of this enum constant, exactly as it is declared in its enum declaration.
final int ordinal()
Returns the ordinal value of this enum constant (i.e., its position in its enum type declaration). The first enum constant is assigned an ordinal value of zero. If the ordinal value of an enum constant is less than the ordinal value of another enum constant of the same enum type, the former occurs before the latter in the enum type declaration.
Note that the equality test implemented by the equals() method is based on reference equality (==) of the enum constants, not on object value equality. Comparing two enum references for equality meanss determining whether they store the reference value of the same enum constant—that is, whether the references are aliases. Thus for any two enum references, meal1 and meal2, the expressions meal1.equals(meal2) and meal1 == meal2 are equivalent.
The java.lang.Enum class also overrides the toString() method from the Object class. The toString() method returns the name of the enum constant, but it is not final and can be overridden by an enum type—but that is rarely done. Examples in this subsection illustrate the use of these methods.
Leave a Reply