java.lang

Class Enum<E>

public class Enum<E extends Enum<E>> extends Object implements Comparable<E>, Serializable

This is the common base class of all Java language enumeration types.

Since: 1.5

Version: 1.1, 01/27/03

Author: Neal Gafter

Field Summary
Stringname
The name of this enum constant, as declared in the enum declaration.
intordinal
The ordinal of this enumeration constant (its position in the enum declaration, where the initial constant is assigned an ordinal of zero).
Constructor Summary
protected Enum(String name, int ordinal)
Sole constructor.
Method Summary
protected Objectclone()
Throws CloneNotSupportedException.
intcompareTo(E o)
Compares this enum with the specified object for order.
booleanequals(Object other)
Returns true if the specified object is equal to this enum constant.
List<E>family()
Returns an immutable list of all the enum constants in this enum constant's enum class.
inthashCode()
Returns a hash code for this enum constant.
protected ObjectreadResolve()
This method ensures proper deserialization of enum constants.
StringtoString()
Returns the name of this enum constant, as contained in the declaration.
protected static <E extends Enum<E>> EvalueOf(List<E> family, String name)
This helper function exists in the prototype only; it will not appear in the final implementation.

Field Detail

name

public final String name
The name of this enum constant, as declared in the enum declaration. Most programmers should use the toString method rather than accessing this field.

ordinal

public final transient int ordinal
The ordinal of this enumeration constant (its position in the enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this field. It is designed for use by sophisticated enum-based data structures, such as java.util.EnumSet and java.util.EnumMap.

Constructor Detail

Enum

protected Enum(String name, int ordinal)
Sole constructor. Programmers cannot invoke this constructor. It is for use by code emitted by the compiler in response to enum class declarations.

Parameters: name - The name of this enum constant, which is the identifier used to declare it. ordinal - The ordinal of this enumeration constant (its position in the enum declaration, where the initial constant is assigned an ordinal of zero).

Method Detail

clone

protected final Object clone()
Throws CloneNotSupportedException. This guarantees that enums are never cloned, which is necessary to preserve their "singleton" status.

Returns: (never returns)

compareTo

public int compareTo(E o)
Compares this enum with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Enum constants are only comparable to other enum constants of the same enum class. The natural order implemented by this method is the order in which the constants are declared.

equals

public final boolean equals(Object other)
Returns true if the specified object is equal to this enum constant.

Parameters: o the object to be compared for equality with this object.

Returns: true if the specified object is equal to this enum constant.

family

public List<E> family()
Returns an immutable list of all the enum constants in this enum constant's enum class. This should be abstract, but isn't due to a bug in the generic compiler.

Returns: an immutable list of all of the enum constants in this enum constant's enum class.

hashCode

public final int hashCode()
Returns a hash code for this enum constant.

Returns: a hash code for this enum constant.

readResolve

protected final Object readResolve()
This method ensures proper deserialization of enum constants.

UNKNOWN: the canonical form of this desesrialized enum const.

toString

public String toString()
Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn't necessary or desirable. An enum class should override this method when a more "programmer-friendly" string form exists.

Returns: the name of this enum constant

valueOf

protected static <E extends Enum<E>> E valueOf(List<E> family, String name)
This helper function exists in the prototype only; it will not appear in the final implementation. It is a shortcut to help implement Enumtype.valueOf(String).