Java Reference
In-Depth Information
EnumConstant
EnumConstants
,
EnumConstant
EnumConstant:
Annotations
opt
Identifier Arguments
opt
ClassBody
opt
Arguments:
(
ArgumentList
opt
)
EnumBodyDeclarations:
;
ClassBodyDeclarations
opt
An enum constant may optionally be preceded by annotation modifiers. If an annotation
a
)annotation
m
that corresponds to
java.lang.annotation.Target
, then
m
must have an element
whose value is
java.lang.annotation.ElementType.FIELD
, or a compile-time error occurs.
The
Identifier
in a
EnumConstant
may be used in a name to refer to the enum constant.
An enum constant may be followed by arguments, which are passed to the constructor of
the enum type when the constant is created during class initialization as described later in
this section. The constructor to be invoked is chosen using the normal overloading rules
The optional class body of an enum constant implicitly defines an anonymous class de-
governed by the usual rules of anonymous classes; in particular it cannot contain any con-
structors.
Instance methods declared in these class bodies may be invoked outside the enclosing
enum type only if they override accessible methods in the enclosing enum type.
It is a compile-time error for the class body of an enum constant to declare an abstract
method.
Because there is only one instance of each enum constant, it is permissible to use the
==
operator in place of the
equals
method when comparing two object references if it is known
that at least one of them refers to an enum constant.
The
equals
method in
Enum
is a
final
method that merely invokes
super.equals
on its argu-
ment and returns the result, thus performing an identity comparison.