Java Reference
In-Depth Information
Each enum type is a special type of class, and the values belonging to the enum type are
(special types of) objects of that class. For example, Grades is, in fact, a class and A , B , C ,
D , and F are public static reference variables to objects of the type Grades .
After an enum type is defined, you can declare reference variables of that type. For
example, the following statement declares myGrade to be a reference variable of the
Grades type:
Grades myGrade;
Because each of the variables A , B , C , D , and F is public and static , they can be
accessed using the name of the class and the dot operator. Therefore, the following
statement assigns the object B to myGrade :
myGrade = Grades.B;
The output of the statement:
System.out.println("myGrade: " + myGrade);
is:
myGrade: B
Similarly, the output of the statement:
System.out.println("Grades.B: " + Grades.B);
is:
Grades.B: B
Each enum constant in an enum type has a specific value, called the ordinal value. The
ordinal value of the first enum constant is 0 , the ordinal value of the second enum constant
is 1, and so on. Therefore, in the enum type Grades , the ordinal value of A is 0 and the
ordinal value of C is 2 .
Associated with enum type is a set of methods that can be used to work with enum types.
Table D-5 describes some of those methods.
TABLE D-5 Methods Associated with enum Types
Method
Description
ordinal ()
Returns the ordinal value of an enum constant
name ()
Returns the name of the enum value
values ()
Returns the values of an enum type as a list
Search WWH ::




Custom Search