7 Usages of Java enum Enumeration Types

Keywords: Java JDK

DK 1.5 introduces a new type, enumeration. Although it is a "small" function in Java, it brings "big" convenience to my development.

Usage 1: Constants

Before JDK 1.5, we defined constants as: publicstaticfianl.... Now, with enumeration, you can group related constants into an enumeration type, and enumeration provides more methods than constants.

 

  1. public enum Color {  
  2.   RED, GREEN, BLANK, YELLOW  
  3. }  

 

Usage 2:switch

The switch statements before JDK 1.6 only support int,char,enum types. Enumeration can make our code more readable.

 

  1. enum Signal {  
  2.     GREEN, YELLOW, RED  
  3. }  
  4. public class TrafficLight {  
  5.     Signal color = Signal.RED;  
  6.     public void change() {  
  7.         switch (color) {  
  8.         case RED:  
  9.             color = Signal.GREEN;  
  10.             break;  
  11.         case YELLOW:  
  12.             color = Signal.RED;  
  13.             break;  
  14.         case GREEN:  
  15.             color = Signal.YELLOW;  
  16.             break;  
  17.         }  
  18.     }  
  19. }  

 

Usage 3: Add a new method to the enumeration

If you want to customize your method, you must add a semicolon at the end of the Enum instance sequence. Furthermore, Java requires that enum instances must be defined first.

 

  1. public enum Color {  
  2.     RED("gules"1), GREEN("green"2), BLANK("white"3), YELLO("yellow"4);  
  3.     //Membership variables  
  4.     private String name;  
  5.     private int index;  
  6.     //Construction method  
  7.     private Color(String name, int index) {  
  8.         this.name = name;  
  9.         this.index = index;  
  10.     }  
  11.     //General methods  
  12.     public static String getName(int index) {  
  13.         for (Color c : Color.values()) {  
  14.             if (c.getIndex() == index) {  
  15.                 return c.name;  
  16.             }  
  17.         }  
  18.         return null;  
  19.     }  
  20.     //get set method  
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.     public int getIndex() {  
  28.         return index;  
  29.     }  
  30.     public void setIndex(int index) {  
  31.         this.index = index;  
  32.     }  
  33. }  

 

Usage 4: Overlay Enumeration Method

An example of toString() method coverage is given below.

 

  1. public enum Color {  
  2.     RED("gules"1), GREEN("green"2), BLANK("white"3), YELLO("yellow"4);  
  3.     //Membership variables  
  4.     private String name;  
  5.     private int index;  
  6.     //Construction method  
  7.     private Color(String name, int index) {  
  8.         this.name = name;  
  9.         this.index = index;  
  10.     }  
  11.     //Coverage method  
  12.     @Override  
  13.     public String toString() {  
  14.         return this.index+"_"+this.name;  
  15.     }  
  16. }  

 

Usage 5: Implementing interfaces

All enumerations inherit from the java.lang.Enum class. Since Java does not support multiple inheritance, enumerated objects can no longer inherit other classes.

 

  1. public interface Behaviour {  
  2.     void print();  
  3.     String getInfo();  
  4. }  
  5. public enum Color implements Behaviour{  
  6.     RED("gules"1), GREEN("green"2), BLANK("white"3), YELLO("yellow"4);  
  7.     //Membership variables  
  8.     private String name;  
  9.     private int index;  
  10.     //Construction method  
  11.     private Color(String name, int index) {  
  12.         this.name = name;  
  13.         this.index = index;  
  14.     }  
  15. //Interface method  
  16.     @Override  
  17.     public String getInfo() {  
  18.         return this.name;  
  19.     }  
  20.     //Interface method  
  21.     @Override  
  22.     public void print() {  
  23.         System.out.println(this.index+":"+this.name);  
  24.     }  
  25. }  

 

Usage 6: Organize enumerations using interfaces

 

  1. public interface Food {  
  2.     enum Coffee implements Food{  
  3.         BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO  
  4.     }  
  5.     enum Dessert implements Food{  
  6.         FRUIT, CAKE, GELATO  
  7.     }  
  8. }  

 

Usage 7: About the Use of Enumerated Sets

java.util.EnumSet and java.util.EnumMap are two sets of enumerations. EnumSet guarantees that elements in a collection are not duplicated; key s in EnumMap are enum types and value s can be arbitrary types. The use of these two sets is not covered here, you can refer to the JDK documentation.

For details and principles of enumeration, please refer to:

Reference: Thinking In Java, 4th Edition

http://softbeta.iteye.com/blog/1185573


Posted by newhen on Sat, 22 Jun 2019 13:59:51 -0700