That said, an object-oriented implementation of the concept of enum in Java can be much more powerful than C-style enum.
The Enum framework (in reality, the whole framework is a single class) proves that by providing extensive functionality that includes:
Every Enum type is represented by a separate class extending the Enum abstract class (see API)
Either an integer or string value or both for each constant A human-readable description that can be easily internationalized. An enum type is automatically associated with .properties files containing labels of enums in specific languages. All constants of an Enum can be loaded from a .properties file.
public class Color extends EnumThis class is accompanied by a Color.properties file that is located in the same package as the Color class:
{
public static Color RED = new Color();
public static Color GREEN = new Color();
public static Color BLUE = new Color();
}
RED=RedItalian names of colors are in Color_it_IT.properties file:
GREEN=Green
BLUE=Blue
RED=RossoThe following table contains values returned by various methods of Color:
GREEN=Verde
BLUE=Azzurro
Enum provides the following automation:
Color.RED Color.GREEN Color.BLUE intValue() 0 1 2 stringValue() "RED" "GREEN" "BLUE" toString() "Red" "Green" "Blue" toString(Locale.ITALY) "Rosso" "Verde" "Azzurro"
Color allColors[] = (Color[])Enum.enum(Color.class);Enums can be used as types of properties of JavaBeans:This method will return all Colors in the order they are declared: Color.RED, Color.GREEN, Color.BLUEColor green = (Color)Enum.enum(Color.class, "GREEN");This method will the Color constant by its string valueColor blue = (Color)Enum.enum(Color.class, 2);This method will the Color constant by its integer value
public class Crayon {private Color color;
public void setColor(Color color){
this.color = color;
}public Color getColor(){
return color;
}...
}Crayon crayon = new Crayon();
crayon.setColor(Color.GREEN);System.err.println("Questo pastello è " +
crayon.getColor().toString(Locale.ITALY));
public class Gender extends EnumSpanish words for genders are in Gender_es.properties file:
{
public static Gender FEMALE = new Gender("F", "Female");
public static Gender MALE = new Gender("M", "Male");protected Gender(String string, String label){
super(string, label);
}
}
F=FemininoThe default labels are used when the .properties file for the corresponding locale is missing.
M=Masculino
The following table contains values returned by various methods of Gender:
Gender.FEMALE Gender.MALE intValue() 0 1 stringValue() "F" "M" toString() "Female" "Male" toString(new Locale("es", "MX")) "Feminino" "Masculino" toString(Locale.ITALY) "Female" "Male"
public class Order extends Enum {The following table contains values returned by various methods of Order:public static Order LESS_THAN = new Order(-1);
public static Order EQUAL_TO = new Order(0);
public static Order GREATER_THAN = new Order(1);protected Order(int integer){
super(integer);
}
}
Order.LESS_THAN Color.EQUAL_TO Color.GREATER_THAN intValue() -1 0 1 stringValue() "LESS_THAN" "EQUAL_TO" "GREATER_THAN" toString() "LESS_THAN" "EQUAL_TO" "GREATER_THAN"
public class Employment extends Enum {String values and labels of the constants can be acquired using the following calls:public static final int EMPLOYED = 1;
public static final int SELF_EMPLOYED = 2;
public static final int UNEMPLOYED = 3;static {
// Register all 'public static ints' of this class as Enums
initIntegerEnum(Employment.class);
}
}
Enum employed = Enum.enum(Employment.class, Employment.EMPLOYED);In this case both the string value and the label will be "EMPLOYED". Just like any Enum, such integer Enum can be internationalized - the public static int field names function as keys in the .properties file.
String string = employed.stringValue();
String label = employed.toString();
The following table contains values returned by various methods of Order:
Enum.enum(
Employment.class,
Employment.EMPLOYED)Enum.enum(
Employment.class,
Employment.UNEMPLOYED)intValue() 1 3 stringValue() "EMPLOYED" "UNEMPLOYED" toString() "EMPLOYED" "UNEMPLOYED"
public class Flower extends EnumThe Flower.properties file:
{
static {
// Load all Enums of this class from Flower.properties
loadEnum(Flower.class);
}// Define any of the Flower constants individually
public static Flower ROSE = enum("rose");/**
* Convenience method that finds a Flower object by its name.
*/
public static Flower enum(String name){
return (Flower)enum(Flower.class, name);
}
}
rose=RosaAn integer Enum can be loaded the same way, except instead of the loadEnum method the loadIntegerEnum method should be called. Property names in the .properties file should then be numeric.
daisy=Chrysanthemum leucanthemum
violet=Viola
forget-me-not=Myosotis
Note: if you are using one of the load... methods, do not allocate any
additional constants, find the loaded once like we did in the Flower class.
JavaDoc
Download ZIP (includes JAR, docs and source)
Enjoy!
Java, JavaSoft, are trademarks or registered trademarks of Sun Microsystems, Inc.