Java Reference
In-Depth Information
Table 18-1. List of Methods in the Enum Class That Are Available in All Enum Types
Method Name
Description
public final String name()
Returns the name of the enum constant exactly as declared in the
enum type declaration.
public final int ordinal()
Returns the order (or position) of the enum constant as declared in
the enum type declaration.
public final boolean equals(Object
other)
Returns true if the specified object is equal to the enum constant.
Otherwise, it returns false. Note that an enum type cannot be
instantiated directly and it has a fixed number of instances, which are
equal to the number of enum constants it declares. It implies that the
== operator and the equals() method return the same result, when
they are used on two enum constants.
public final int hashCode()
Returns the hash code value for an enum constant.
public final int compareTo(E o)
Compares the order of this enum constant with the order of the
specified enum constant. It returns the difference in ordinal value
of this enum constant and the specified enum constant. Note that to
compare two enum constants they must be of the same enum type.
Otherwise, a runtime exception is thrown.
public final Class<E>
getDeclaringClass()
Returns the class object for the class that declares the enum constant.
Two enum constants are considered to be of the same enum type if
this method returns the same class object for both. Note that the class
object returned by the getClass() method, which every enum type
inherits from the Object class, might not be the same as the class object
returned by this method. When an enum constant has a body, the
actual class of the object for that enum constant is not the same as the
declaring class; actually, it is one of the subclasses of the declaring class.
public String toString()
By default, it returns the name of the enum constant, which is the
same as the return value of the name() method. Note that this method
is not declared final and hence you can override it to return a more
meaningful string representation for each enum constant.
public static <T extends Enum<T>>T
valueOf(Class<T> enumType, String
name)
Returns an enum constant of the specified enum type and name. For
example, you can use the following code to get the LOW enum constant
value of the Severity enum type in your code:
Severity lowSeverity = Enum.valueOf(Severity.class, "LOW")
protected final Object clone() throws
CloneNotSupportedException
The Enum class redefines the clone() method. It declares the method
final , so it cannot be overridden by any enum type. The method
always throws an exception. This is done intentionally to prevent
cloning of enum constants. This makes sure that only one set of enum
constants exists for each enum type.
protected final void finalize()
The Enum class declares it final so that it cannot be overridden by any
enum type. It provides an empty body. Since you cannot create an
instance of an enum type, except its constants, it makes no sense to
have a finalize() method for your enum type.
 
 
Search WWH ::




Custom Search