Java Reference
In-Depth Information
have to write the name strings yourselfinvoking thename method on an
enum constant will return its name as a String .
You can get a feel for how an enum is internally defined from the fol-
lowing pseudo-code, which shows a mock class definition for the above
Suit enum:
class Suit extends Enum<Suit> // pseudo-code only
implements Comparable<Suit>, Serializable {
public static final Suit CLUBS = new Suit("CLUBS");
public static final Suit DIAMONDS = new
Suit("DIAMONDS");
public static final Suit HEARTS = new Suit("HEARTS");
public static final Suit SPADES = new Suit("SPADES");
String name;
Suit(String name) { this.name = name; }
public String toString() { return name; }
// ... compiler generated methods ...
}
There is a bit more to the detail, but the above gives you the general
idea.
The constructor to invoke is determined according to the usual rules for
matching argument types with constructor parameter typessee " Finding
the Right Method " on page 224 .
There are three restrictions on the definition of an enum constructor:
All enum constructors are private. While you can use the private
access modifier in the constructor declaration, by convention it is
omitted. Private constructors ensure that an enum type cannot
be instantiated directly.
 
Search WWH ::




Custom Search