CLASSES(8)
CLASSES(8)
ENUMS
The notation form for enum type is:
ClassModifiersopt enum Identifier Interfacesopt EnumBody
Where Enum body is:
{ EnumConstantsopt,opt EnumBodyDeclarationsopt }
The body of an enum type may contain enum constants which defines instances of the enum type.
Notes:
1. never explicitly instantiate an enum type
2. enum constants can never be cloned due the the clone method of Enum
3. the special treatment of the serialization mechanism ensures that duplicate instances are never created as a result of deserialisation.
4. reflective instantiation of enum types is prohibited.
Notes 1-4 above ensures that only an enum constant can define instances of enum types.
Enum constants thus have the following notation:
Annotations Identifier Argumentsopt ClassBodyopt
Where Arguments: (ArgumentListopt)
Also the optional enum body declaration is declared thus: ;ClassBodyDeclarationsopt
An enum constant when preceded by annotation modifiers, say a, corresponding to an annotation type T, and T has a (meta-)annotation m such that the m corresponds to annotation.Target, then m must have an element whose value is annotation.ElementType.FIELD, or a compile time error results.
An enum constant may be followed by arguments and these arguments are passed to the constructor of the enum type when the constant is created during class initialization. If the arguments are omitted, an empty argument list is assumed. If the enum type has no constructor declarations, a parameterless default constructor is provided (matching the implicit empty argument list) and the default constructor is private.
The optional class body of an enum constant implicitly defines an anonymous class declaration that extends the enclosing enum type. The class body is governed by the usual anonymous class rules, in particular, it cannot contain constructors.
Compile time error scenarios, brief:
1. a compile time error if an enum type is declared abstract
2. an enum type, E, cannot have abstract methods unless E has one or more enum constants and all these enum constants have class bodies that provide concrete implementations of the abstract method.
3. the class body of an enum constant cannot declare an abstract method.
An enum type is implicitly final unless it contains an enum constant that has a class body, but does not have to be explicitly so defined otherwise compile time error.
Nested enum types are implicitly static and one can explicitly declare them thus.
Any constructor or member declaration declared with an enum declaration applies to the enum type exactly as if they had been present in the class body of a normal class declaration unless explicitly stated otherwise.
The direct superclass of an enum type named E is Enum
The following implicitly declared static method exists for any enum type:
public static E[] values(); //this returns an array containing the constants of the enum
type in the order declared.
public static E valueOf(String name); //returns the enum constant with the specified
name
read the specific examples in the jls!

No comments:
Post a Comment