26. Java Autoboxing and Enumeration

26.2. Enumeration

 

 

A new enumeration construct was included in Java 5.0 to make it simpler to repre- sent a finite list of named values as a type. The enum keyword was added as part of this construct. Standard examples of lists of values appropriate for enumera- tions are the days of the week, months of the year, the four seasons, the planets, the four suits in a deck of cards, and the ranks of cards in a deck of cards. The following declaration of Season enumerated type is an example used by the Sun online documentation.

,,

 

J

Compiling a file that contains only this statement will create a Season.class file that defines a Java type just in the same way that compiling class definitions does. The variables and values of type Season can be referred to in other classes

 

just like other types and values. For example, the following statements are valid statements in a method definition in another class:

,,

 

 

J

Note that the values of enumerated types can be used in assignment statements, equality relations, and it will be printed out exactly as declared in the enum statement.

The enum declaration could also occur inside the definition of a class and be declared as either public or private. In this case the visibility of the type would be determined in a manner similar to inner classes.

A standard way to represent such a finite list of values in Java before the enum construct was to create a list of constants of type int. For example, if one wanted to represent the four seasons you would have to do it inside a definition of a class, say of a class named Calendar. Such a representation might look like:

,,

 

 

 

 

 

 

J

In addition to being a lengthier declaration, note that other classes that wish to refer to this representation would have to use notation something like:

,,

 

 

J

In addition to being more awkward, note that the println() call will print out an integer in this case. Some additional code would have to be written to be able to print the names of the seasons from the int values used to represent them. It is the case that for methods in the Calendar class, the names of the constants look very much like the values of the enum type.

 

To illustrate a couple of additional advantages of the enum structure, lets con- sider using the int representation above in a method definition that describes the start date of a given season. Code for such a method would look something like:

,,

 

 

 

 

 

 

J

This method has a problem referred to as not being typesafe. We would want the startDate() method to be called only with an argument equal to an int value of 0, 1, 2, or 3. There is no way to tell the compiler to make sure that other int values are not used as an argument to this method.

Let’s contrast this with a similar startDate() method that can refer to the Season enumerated type that was defined above. The Calendar class (Figure G– 1) shows the definition of a startDate() method as well as a method to print a list of seasons with corresponding starting dates. Note that the parameter of

 

 

,,

 

 

 

 

 

 

 

 

 

 

 

J

Figure G–1: A Calendar class using the Season.

 

 

startDate() is of type Season and so the Java compiler can check that call to this method have an argument of this type. This time the startDate() is typesafe.

The printDates() method illustrates another feature of the enumeration structure. The for loop in this method is the for-in loop which was added to Java 5.0. The expression Season.values() denotes a list of the elements of the type in the order that they were declared. The for-in loop iterates through all the values of the type in the correct order and, in this case, prints out the type

 

name followed by a dash followed by the String computed by the startDate() method. The output when the printDates() method is called is given below:

,,

 

 

 

J

The for-in loop provides a very nice way to iterate through the values of any enumerated type. You may wish to write a corresponding method for the earlier int representation of the seasons for a comparison.

Sun’s online Java 5.0 documentation provides a more precise definition of enu- merated types and describes quite a number of other features that we have not alluded to.

 

 

 

 

 

 

 

 

 

 

 

Appendix H