Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Enum


As described in Chapter 12, enumerations were recently added to the Java language. (Recall that
an enumeration is created by using the keywordenum.) All enumerations automatically inherit
Enum.Enumis a generic class that is declared as shown here:

class Enum<E extends Enum<E>>

Here,Estands for the enumeration type.Enumhas no public constructors.
Enumdefines several methods that are available for use by all enumerations, which are
shown in Table 16-21.

432 Part II: The Java Library


Method Description
int getLineNumber( ) Returns the source-code line number of the execution point described
by the invokingStackTraceElement. In some situations, the line
number will not be available, in which case a negative value is
returned.
String getMethodName( ) Returns the method name of the execution point described by the
invokingStackTraceElement.
int hashCode( ) Returns the hash code for the invokingStackTraceElement.
boolean isNativeMethod( ) Returnstrueif the invokingStackTraceElementdescribes a native
method. Other wise, returnsfalse.
String toString( ) Returns theStringequivalent of the invoking sequence.

TABLE 16-20 The Methods Defined byStackTraceElement(continued)

Method Description
protected final Object clone( )
throws CloneNotSupportedException

Invoking this method causes a
CloneNotSupportedExceptionto be thrown. This
prevents enumerations from being cloned.
final int compareTo(Ee) Compares the ordinal value of two constants of the
same enumeration. Returns a negative value if the
invoking constant has an ordinal value less thane’s,
zero if the two ordinal values are the same, and a
positive value if the invoking constant has an ordinal
value greater thane’s.
final boolean equals(Objectobj) Returns true ifobjand the invoking object refer
to the same constant.
final Class<E> getDeclaringClass( ) Returns the type of enumeration of which the
invoking constant is a member.
final int hashCode( ) Returns the hash code for the invoking object.

TABLE 16-21 The Methods Defined byEnum
Free download pdf