Java Reference
In-Depth Information
An enum cannot be declared to
extend
another type because all enums
implicitly extend
java.lang.Enum
, which we discuss a little later. Nor can
any other type extend an enum (not even another enum), because all
enum types act as if they are implicitly
final
. An enum can declare that
it implements one or more interfacesin fact all enums are implicitly
Seri-
The possible members of an enum include all the class members: fields,
methods, and nested types, including nested enumsthough it should be
rare to require such sophistication: The main attraction of enums is their
simplicity. The enum constants themselves are implicitly static fields
with the same type as the enum.
Every enum type
E
has two static methods that are automatically gen-
erated for it by the compiler:
public static
E
[]
values()
Returns an array containing each of the enum constants in the
order in which they were declared.
public static
E
valueOf(String name)
Returns the enum constant with the given name. If the name
does not match an enum constant name exactly then an
Il-
legalArgumentException
is thrown.
Note that the length of the array returned by
values
tells you how
many enum constants there are in the enum, so the
getSize
method we
demonstrated is not needed in practice.
An enum type is not allowed to override the
finalize
method from
Ob-
ject
. Enum instances may never be finalized (see "
Finalization
"
on page