Enumeration


[Page 838 (continued)]

A new enumeration construct was included in Java 5.0 to make it simpler to represent 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 enumerations are the days of the week, the 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.

public enum Season {spring, summer, fall, winter} 


Compiling a file that contains only this statement will create a Season.class file that defines a Java type 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:

Season s1 = winter; if (s1 == spring)     System.out.println(s1); 


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


[Page 839]

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, to represent the four seasons, you had to do it inside a definition of a class, say of a class named Calendar. Such a representation might look like:

public class Calendar {     public static final int SPRING = 0;     public static final int SUMMER = 1;     public static final int FALL = 2;     public static final int WINTER = 3;     // Other Calendar definitions } // Calendar 


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

int s1 = Calendar.WINTER; if (s1 == Calendar.SPRING)     System.out.println(s1); 


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, let's consider using the int representation shown above in a method definition that describes the start date of a given season. Code for such a method would look something like:

public static String startDate(int s){     switch(s){     case SPRING : return "Vernal Equinox";     case SUMMER : return "Summer Solstice";     case FALL : return "Autumnal Equinox";     case WINTER : return "Winter Solstice";     } // switch     return "error"; } // startDate() 


This method has a problem referred to as not being typesafe. We would want the start-Date() 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.


[Page 840]

Contrast this with a similar startDate() method that can refer to the Season enumerated type 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 startDate() is of type Season, and so the Java compiler can check that calls to this method have an argument of this type. This time the startDate() is typesafe.

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

public class Calendar {     public static String startDate(Season s){         switch(s){         case spring : return "Vernal Equinox";         case summer : return "Summer Solstice";         case fall : return "Autumnal Equinox";         case winter : return "Winter Solstice";         } // switch         return "error";     } // startDate()     public static void printDates(){         for (Season s : Season.values())             System.out.println(s + " - " + startDate(s));     } // printDates() } // Calendar class 

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 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:

spring - Vernal Equinox summer - Summer Solstice fall - Autumnal Equinox winter - Winter Solstice 


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 enumerated types and describes quite a number of other features that we have not alluded to.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net