Java Reference
In-Depth Information
6.2. Enum Declarations
An enum is a special kind of class, but an enum declaration has restric-
tions on its form and use that are different from those of a normal class.
An enum declaration is like a class declaration with two exceptions: The
keyword enum is used instead of class , and before declaring any class
members (or initialization blocks) an enum must first declare all of its
enum constants. If an enum does declare anything other than its enum
constants, then the list of enum constants must be termi ated by a semi-
colon. For example, this variation of Suit provides a method to tell you
how many suits there are:
enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES;
public static int getSize() { return 4; }
}
For convenience, you're allowed to have a comma after the last enum
constant (but before the semicolon if present), whether or not any other
class declarations follow. This is useful for declaring an enum constant
per line:
enum Suit {
CLUBS,
DIAMONDS,
HEARTS,
SPADES,
}
Being able to keep the extraneous comma allows you to reorder lines
without having to remember to add the comma to the old last line or re-
move it from the new last line. (Array initialization lists have the same
featuresee page 175 .)
 
Search WWH ::




Custom Search