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
9.7 ) on an enum constant corresponds to an annotation type T 9.6 ), and T has a (meta-
)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.
The scope and shadowing of an enum constant is specified in § 6.3 and § 6.4 .
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
15.12.2 ) . If the arguments are omitted, an empty argument list is assumed.
The optional class body of an enum constant implicitly defines an anonymous class de-
claration (§ 15.9.5 ) that extends the immediately enclosing enum type. The class body is
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.
Search WWH ::




Custom Search