Java Reference
In-Depth Information
// Create constants when class is loaded
static {
LOW = new Severity("LOW", 0);
MEDIUM = new Severity("MEDIUM", 1);
HIGH = new Severity("HIGH", 2);
URGENT = new Severity("URGENT", 3);
}
// The private constructor to prevent direct instantiation
private Severity(String name, int ordinal) {
super(name, ordinal);
}
public static Severity[] values() {
return new Severity[] { LOW, MEDIUM, HIGH, URGENT };
}
public static Severity valueOf(String name) {
if (LOW.name().equals(name)) {
return LOW;
}
if (MEDIUM.name().equals(name)) {
return MEDIUM;
}
if (HIGH.name().equals(name)) {
return HIGH;
}
if (URGENT.name().equals(name)) {
return URGENT;
}
throw new IllegalArgumentException(
"Invalid enum constant" + name);
}
}
By looking at the transformed code for the Severity enum declaration, the following points can be made:
Every enum type implicitly extends
java.lang.Enum class. This means that all methods
defined in the Enum class can be used with all enum types. Table 18-1 lists the methods that are
defined in the Enum class.
Search WWH ::




Custom Search