: Color enum declared above is final, change that to get this // example to compile. public static final ColorEnum YELLOW = new ExtraColorEnum("Yellow"); private ExtraColorEnum(String color) { super(color); } public static ColorEnum getEnum(String color) { return (ColorEnum) getEnum(ExtraColorEnum.class, color); } public static Map getEnumMap() { return getEnumMap(ExtraColorEnum.class); } public static List getEnumList() { return getEnumList(ExtraColorEnum.class); } public static Iterator iterator() { return iterator(ExtraColorEnum.class); } }
This example will return RED, GREEN, BLUE, YELLOW from the List and iterator methods in that order. The RED, GREEN and BLUE instances will be the same (==) as those from the superclass ColorEnum. Note that YELLOW is declared as a ColorEnum and not an ExtraColorEnum.
Functional Enums
The enums can have functionality by defining subclasses and overriding the getEnumClass()
method:
public static final OperationEnum PLUS = new PlusOperation(); private static final class PlusOperation extends OperationEnum { private PlusOperation() { super("Plus"); } public int eval(int a, int b) { return a + b; } } public static final OperationEnum MINUS = new MinusOperation(); private static final class MinusOperation extends OperationEnum { private MinusOperation() { super("Minus"); } public int eval(int a, int b) { return a - b; } } private OperationEnum(String color) { super(color); } public final Class getEnumClass() { // NOTE: new method! return OperationEnum.class; } public abstract double eval(double a, double b); public static OperationEnum getEnum(String name) { return (OperationEnum) getEnum(OperationEnum.class, name); } public static Map getEnumMap() { return getEnumMap(OperationEnum.class); } public static List getEnumList() { return getEnumList(OperationEnum.class); } public static Iterator iterator() { return iterator(OperationEnum.class); } }
The code above will work on JDK 1.2. If JDK1.3 and later is used, the subclasses may be defined as anonymous.
Nested class Enums
Care must be taken with class loading when defining a static nested class for enums. The static nested class can be loaded without the surrounding outer class being loaded. This can result in an empty list/map/iterator being returned. One solution is to define a static block that references the outer class where the constants are defined. For example:
public final class Outer { public static final BWEnum BLACK = new BWEnum("Black"); public static final BWEnum WHITE = new BWEnum("White"); // static nested enum class public static final class BWEnum extends Enum { static { // explicitly reference BWEnum class to force constants to load Object obj = Outer.BLACK; } // ... other methods omitted } }
Although the above solves the problem, it is not recommended. The best solution is to define the constants in the enum class, and hold references in the outer class:
public final class Outer { public static final BWEnum BLACK = BWEnum.BLACK; public static final BWEnum WHITE = BWEnum.WHITE; // static nested enum class public static final class BWEnum extends Enum { // only define constants in enum classes - private if desired private static final BWEnum BLACK = new BWEnum("Black"); private static final BWEnum WHITE = new BWEnum("White"); // ... other methods omitted } }
For more details, see the 'Nested' test cases.
@author Apache Avalon project
@author Stephen Colebourne
@author Chris Webb
@author Mike Bowler
@author Matthias Eichel
@since 2.1 (class existed in enum package from v1.0)
@version $Id: Enum.java 161243 2005-04-14 04:30:28Z ggregory $