The ordinal number simply tells the position the value the list
CHAPTER 2. NAMES AND THINGS 35
2.3.3 Introduction to Enums
enum ⟨enum-type-name ⟩ { ⟨list-of-enum-values ⟩ }
This definition cannot be inside a subroutine. You can place it outside the main() routine of the program. The ⟨enum-type-name⟩ can be any simple identifier. This identifier becomes the name of the enum type, in the same way that “boolean” is the name of the boolean type and “String” is the name of the String type. Each value in the ⟨list-of-enum-values⟩ must be a simple identifier, and the identifiers in the list are separated by commas. For example, here is the definition of an enum type named Season whose values are the names of the four seasons of the year:
Season vacation;
After declaring the variable, you can assign a value to it using an assignment statement. The value on the right-hand side of the assignment can be one of the enum constants of type Season.
Because an enum is technically a class, the enum values are technically objects. As ob-jects, they can contain subroutines. One of the subroutines in every enum value is named ordinal(). When used with an enum value, it returns the ordinal number of the value in the list of values of the enum. The ordinal number simply tells the position of the value in the list. That is, Season.SPRING.ordinal() is the int value 0, Season.SUMMER.ordinal() is 1, Season.FALL.ordinal() is 2, and Season.WINTER.ordinal() is 3. (You will see over and over again that computer scientists like to start counting at zero!) You can, of course, use the ordinal() method with a variable of type Season, such as vacation.ordinal() in our example.
Right now, it might not seem to you that enums are all that useful. As you work though the rest of the book, you should be convinced that they are. For now, you should at least appreciate them as the first example of an important concept: creating new types. Here is a little example that shows enums being used in a complete program:
public static void main(String[] args) {
Day tgif; // Declare a variable of type Day. Month libra; // Declare a variable of type Month.
| // Output value will be: |
|
|---|
System.out.print("That’s the ");
System.out.print( libra.ordinal() );
System.out.println("-th month of the year.");
System.out.println(" (Counting from 0, of course!)");
| // Output value will be: |
|---|
}
}


