Java Reference
In-Depth Information
•
enumType
identifies the
Class
object of the enum from which to return a
constant.
•
name
identifies the name of the constant to return.
Forexample,
Coin penny = Enum.valueOf(Coin.class, "PENNY");
assigns the
Coin
constant whose name is
PENNY
to
penny
.
Youwill not discover a
values()
method in
Enum
'sJava documentation because
the compiler
synthesizes
(manufactures) this method while generating the class.
Extending the Enum Class
Enum
's generic type is
Enum<E extends Enum<E>>
. Although the formal type
parameter list looks ghastly, it is not that hard to understand. But first, take a look at
Listing 3-66.
The
Coin
class as it appears from the perspective of its classfile
final class Coin extends Enum<Coin>
{
public static final Coin PENNY = new Coin("PENNY", 0);
public static final Coin ICKEL = new Coin("NICKEL", 1);
public static final Coin DIME = new Coin("DIME", 2);
public static final Coin QUARTER = new Coin("QUARTER",
3);
private static final Coin[] $VALUES = { PENNY, ICKEL,
DIME, QUARTER };
public static Coin[] values()
{
return Coin.$VALUES.clone();
}
public static Coin valueOf(String name)
{
return Enum.valueOf(Coin.class, "Coin");
}
private Coin(String name, int ordinal)
{
super(name, ordinal);
}
}