Java Reference
In-Depth Information
MORE ON ENUMERATIONS
When I introduced enumerations in Chapter 2, I said that there was more to enumerations than simply a type
with a limited range of integer values. In fact, an enumeration type is a special form of class. When you
define an enumeration type in your code, the enumeration constants that you specify are created as instances
of a class that has the Enum class, which is defined in the java.lang package, as a superclass. The object
that corresponds to each enumeration constant stores the name of the constant in a field, and the enumera-
tion class type inherits the toString() method from the Enum class. The toString() method in the Enum
class returns the original name of the enumeration constant, so that's why you get the name you gave to an
enumeration constant displayed when you output it using the println() method.
You have seen that you can put the definition of an enumeration type in the definition of a class. You can
also put the definition in a separate source file. In this case you specify the name of the file containing the
enumeration type definition in the same way as for any other class type. An enumeration that you define
in its own source file can be accessed by any other source file in exactly the same way as any other class
definition.
An object representing an enumeration constant also stores an integer field. By default, each constant in
an enumeration is assigned an integer value that is different from all the others. The values are assigned to
the enumeration constants in the sequence in which you specify them, starting with zero for the first con-
stant, one for the second, and so on. You can retrieve the value for a constant by calling its ordinal() meth-
od, but you should not need to do this in general.
You have already seen in Chapter 3 that you can compare values of an enumeration type for equality
using the equals() method. For example, assuming that you have defined an enumeration type, Season ,
with enumeration constants spring , summer , fall , and winter , you could write the following:
Season now = Season.winter;
if(now.equals(Season.winter))
System.out.println("It is definitely winter!");
The equals() method is inherited from the Enum class in your enumeration class type. Your enumeration
class type also inherits the compareTo() method that compares instances of the enumeration based on their
ordinal values. It returns a negative integer if the value for the instance for which the method is called is less
than the instance that you pass as the argument, 0 if they are equal, and a positive integer if the value of the
current instance is greater than the value for the argument. Thus, the sequence in which you specify the enu-
meration constants when you define them determines the order that the compareTo() method implements.
You might use it like this:
if(now.compareTo(Season.summer) > 0)
System.out.println("It is definitely getting colder!");
The values() method for an enumeration that I introduced in Chapter 3 is a static member of your enu-
meration class type. This method returns a collection object containing all the enumeration constants that
you can use in a collection-based for loop. You learn about collection classes in Chapter 14.
Adding Members to an Enumeration Class
Because an enumeration is a class, you have the possibility to add your own methods and fields when you
define the enumeration type. You can also add your own constructors to initialize any additional fields you
Search WWH ::




Custom Search