Enum<E extends Enum<E>>


This class is the common superclass of all enumerated types. It is not itself an enum type, however, and a Java compiler does not allow other classes to extend it. Subclasses of Enum may be only created with enum declarations. Enum is a generic type, and the type variable E represents the concrete enumerated type that actually extends Enum. This type variable exists so that Enum can implement Comparable<E>.

Every enumerated constant has a name (the name it was declared with) and an ordinal value—the first constant in an enum declaration has an ordinal of 0, the second has an ordinal of 1, and so on. The final methods name( ) and ordinal( ) return these values. Most users of enumerated constants will use toString( ) instead of name( ). The implementation of toString( ) defined by Enum returns the same value as name( ). The toString( ) method is not final, however, and it can be overridden in enum declarations.

Enum implements a number of Object and Comparable methods and makes its implementations final so that they are inherited by all enum types and may not be overridden. equals( ) compares enumerated constants with the = = operator, and hashCode( ) returns the System.identityHashCode( ) value. In order to make this identity-based equals( ) implementation work, Enum overrides the protected clone( ) method to throw CloneNotSupportedException, preventing additional copies of enumerated values from being created. Finally, the compareTo( ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.