Java Reference
In-Depth Information
}
the class
PackageOnly
can be instantiated only within the package
just
, in which it is de-
clared.
8.9. Enums
An
enum declaration
specifies a new enum type.
EnumDeclaration:
ClassModifiers
opt
enum
Identifier Interfaces
opt
EnumBody
EnumBody:
{
EnumConstants
opt
,
opt
EnumBodyDeclarations
opt
}
An enum type is implicitly
final
unless it contains at least one enum constant that has a class
body.
It is a compile-time error to explicitly declare an enum type to be
final
.
Nested enum types are implicitly
static
. It is permissible to explicitly declare a nested enum
type to be
static
.
This implies that it is impossible to define a local (§
14.3
) enum, or to define an enum
in an inner class (§
8.1.3
).
An enum type has no instances other than those defined by its enum constants. It is a
The
final clone
method in
Enum
ensures that enum constants can never be cloned, and
the special treatment by the serialization mechanism ensures that duplicate instances
are never created as a result of deserialization. Reflective instantiation of enum types
is prohibited. Together, these four things ensure that no instances of an enum type ex-
ist beyond those defined by the enum constants.
8.9.1. Enum Constants
The body of an enum type may contain
enum constants
. An enum constant defines an in-
stance of the enum type.
EnumConstants: