JDK 1.5 introduces a new type -- enumeration. In Java, it's just a small feature, but it can bring us much convenience.Here we summarize some use cases of Java enumeration.
1. Constant
Prior to JDK 1.5, we can define constant as public static final..., now we can use enumeration to group all constants in one enum variable and it also provides some useful functions.
- public enum Color {
- RED, GREEN, BLANK, YELLOW
- }
2.In Switch
Prior to JDK 1.6, only int,char and enum are supported in switch statement, with enum, we can write more readable codes.
- enum Signal {
- GREEN, YELLOW, RED
- }
- public class TrafficLight {
- Signal color = Signal.RED;
- public void change() {
- switch (color) {
- case RED:
- color = Signal.GREEN;
- break;
- case YELLOW:
- color = Signal.RED;
- break;
- case GREEN:
- color = Signal.YELLOW;
- break;
- }
- }
- }
3. Add new methods in enum
If we want to define our own methods, then we have to add a semicolon after the implementation of enum variable.
- public enum Color {
- RED("RED", 1), GREEN("GREEN", 2), BLANK("WHITE", 3), YELLO("YELLOW", 4);
- // Member property
- private String name;
- private int index;
- // Constructor
- private Color(String name, int index) {
- this.name = name;
- this.index = index;
- }
- // Method
- public static String getName(int index) {
- for (Color c : Color.values()) {
- if (c.getIndex() == index) {
- return c.name;
- }
- }
- return null;
- }
- // get set methods
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getIndex() {
- return index;
- }
- public void setIndex(int index) {
- this.index = index;
- }
- }
4. Override methods in enum
Here is an example of overriding toString() method.
- public enum Color {
- RED("RED", 1), GREEN("GREEN", 2), BLANK("WHITE", 3), YELLO("YELLOW", 4);
- // Member property
- private String name;
- private int index;
- // Constructor
- private Color(String name, int index) {
- this.name = name;
- this.index = index;
- }
- //Override method
- @Override
- public String toString() {
- return this.index+"_"+this.name;
- }
- }
5. Implement interface
All enum objects are inherited from java.lang.Enum class. Since Java doesn't support multiple inheritance, so enum cannot inherit other classes.
- public interface Behaviour {
- void print();
- String getInfo();
- }
- public enum Color implements Behaviour{
- RED("RED", 1), GREEN("GREEN", 2), BLANK("WHITE", 3), YELLO("YELLOW", 4);
- // Member property
- private String name;
- private int index;
- // Constructor
- private Color(String name, int index) {
- this.name = name;
- this.index = index;
- }
- //Interface method
- @Override
- public String getInfo() {
- return this.name;
- }
- //Interface method
- @Override
- public void print() {
- System.out.println(this.index+":"+this.name);
- }
- }
6. Use interface to organize enumerations
- public interface Food {
- enum Coffee implements Food{
- BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO
- }
- enum Dessert implements Food{
- FRUIT, CAKE, GELATO
- }
- }
7. About use of enum collection
java.util.EnumSet and java.util.EnumMap are two enumeration collections. EnumSet will guarantee the element in the set is unique. key in EnumMap are enum type, and value can be any data type.
Source : http://softbeta.iteye.com/blog/1185573