THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

To be directly accessible, the methods defined for each enum constant must be declared as methods of the
enum type itselfeither explicitly, as above for reachable, or implicitly by an interface the enum
implements. An enum type is allowed to declare an abstract method, only if there is an implementation of
that method for each and every enum constant.


Exercise 6.5: Redo Exercise 6.4 making getColor an abstract method and defining constant-specific
methods for each enum constant to return the correct Color object. Would you recommend using
constant-specific methods to do this?


6.4. java.lang.Enum


All enum types implicitly extend java.lang.Enum,[3] but no class is allowed to extend Enum directly.
Although Enum does provide some methods that can be useful for working with enums, its main role is to
establish some useful properties for all enum types:


[3]Enum is actually a generic class defined as Enum<T extends Enum<T>>. This
circular definition is probably the most confounding generic type definition you are likely to
encounter. We're assured by the type theorists that this is quite valid and significant, and that
we should simply not think about it too much, for which we are grateful.

The clone method is overridden to be declared final and to throw
CloneNotSupportedExceptionmaking it impossible to ever clone an enum instance.


The hashCode and equals methods are overridden and declared finalensuring consistent and
efficient hashing for enums, and ensuring that equivalence is the same as identity.


The compareTo method of interface java.lang.Comparable is implemented and defined such
that enum constants have a natural ordering based on their order of declarationthe first declared enum
constant has the lowest position in the ordering.


Enum also provides a toString implementation that returns the name of the enum constant, as we have
mentioned, but which you can chose to override. It also provides a finalname method that also returns the
name of the enum constant as a String. The difference between these two methods is that name will return
the exact name of the enum constant, while toString may be overridden to return a more "user friendly"
version of the namefor example, returning "Diamonds" instead of "DIAMONDS".


The additional methods of Enum are:


public final intordinal()

Returns the ordinal value of an enum constant. Enum constants are given
ordinal values based on the order they are declared: the first declared constant
has an ordinal value of zero, the second has an ordinal value of one, etc.

public final Class<E>geTDeclaringClass()

Returns the Class object representing the enum type of this enum constant.
This will be different from the value returned by Object.getClass for
any enum constant that is set to an anonymous inner class object.

The Enum class also provides a static utility method, valueOf, that takes a class object for an enum type,
and a name, and returns the named enum constant from the given enum. If the enum doesn't have a enum
constant by that name then IllegalArgumentException is thrown.

Free download pdf