Detailed explanation of the use and analysis of Java Enum

  • 2020-06-03 06:25:57
  • OfStack

java Enum use


public enum EnumTest {
   FRANK("The given name of me"),
   LIU("The family name of me");
   private String context;
   private String getContext(){
 return this.context;
   private EnumTest(String context){
 this.context = context;
   public static void main(String[] args){
 for(EnumTest name :EnumTest.values()){
 System.out.println(name+" : "+name.getContext());

Analysis of enumeration implementation in Java:


public enum Color{ 

Obviously, enum is a lot like the special class, in that the type defined by the enum declaration is actually a class. These classes are subclasses of the Enum class in the library (java.lang.Enum) < E > ). They inherit many of the useful methods in this Enum. After we compiled the code, we found that the compiler compiled the enum type into a single bytecode file: Color.class.

Color bytecode code

final enum hr.test.Color { 
 //  All enumerated values are class static constants  
 public static final enum hr.test.Color RED; 
 public static final enum hr.test.Color BLUE; 
 public static final enum hr.test.Color BLACK; 
 public static final enum hr.test.Color YELLOW; 
 public static final enum hr.test.Color GREEN; 
private static final synthetic hr.test.Color[] ENUM$VALUES; 
 //  Initializes all enumeration value objects of an enumeration class 1 Initialization time  
 static { 
    0 new hr.test.Color [1]  
   3 dup 
   4 ldc <String "RED"> [16] // Enumerate value strings "RED" Push operand stack  
   6 iconst_0 //  The integer value 0 Push operand stack  
   7 invokespecial hr.test.Color(java.lang.String, int) [17] // call Color A private constructor is created for the class Color object RED 
   10 putstatic hr.test.Color.RED : hr.test.Color [21] // Assign an enumeration object to Color Static constant of RED .  
   .........  The enumeration object BLUE With the  
  102 return 
 //  Private constructor, which cannot be created dynamically externally 1 Enumeration class objects ( That is, it is not possible to create dynamically 1 An enumeration values ) .  
 private Color(java.lang.String arg0, int arg1){ 
   //  Call the parent class Enum Created by the protected constructor 1 Enumeration object  
   3 invokespecial java.lang.Enum(java.lang.String, int) [38] 
 public static hr.test.Color[] values(); 
  //  implementation Enum An abstract method of a class   
 public static hr.test.Color valueOf(java.lang.String arg0); 

Let's look in detail at the characteristics and usage of the enumeration class defined by enum. (Color is used as an example later)

1. Color enumeration class is class, and it is an final class that cannot be inherited. Its enumerated values (RED,BLUE...) Class static constants of type Color, which we can

Get an instance of the Color enumeration class by:

Color c=Color.RED;

Note: These enumeration values are for public static final, which is the way we often define constants, so it is best to use all uppercase enumeration values in an enumeration class.

2. The immediate enumeration class is class, and of course there are constructors, methods, and data fields in the enumeration type. However, the constructor for an enumeration class is quite different:

(1) The constructor is called only when the enumerated value is constructed.

enum Color{ 
        // Construct enumerated values, such as RED(255,0,0) 
        private Color(int rv,int gv,int bv){ 
        public String toString(){ // Overrides the parent class Enum the toString() 
        return super.toString()+"("+redValue+","+greenValue+","+blueValue+")"; 
        private int redValue; // Custom data fields, private To encapsulate.  
        private int greenValue; 
        private int blueValue; 

(2) Constructors can only be private to private, and public constructors are definitely not allowed. This ensures that external code cannot newly construct instances of enumerated classes. This makes perfect sense, because we know that enumerated values are constants for public static final. But enumerating the methods and data fields of a class can allow external access.

public static void main(String args[]) 
    // Color colors=new Color(100,200,300); //wrong 
      Color color=Color.RED; 
      System.out.println(color); //  Call the toString() methods  

3. All enumeration classes inherit from Enum's methods, which are described in detail below.

(1) ordinal() method: Returns the enumeration value in the order of the enumeration class. This order depends on the order in which the enumerated values are declared.

       Color.RED.ordinal(); // Return results: 0
         Color.BLUE.ordinal(); // Return results: 1

(2) compareTo() : Enum implements the java.lang.Comparable interface, so you can compare the order of the image with the specified object. compareTo in Enum returns the difference in order between two enumerated values. Of course, the premise is that both enumeration values must belong to the same enumeration class, otherwise an ClassCastException() exception is thrown. (Specific visible source code)

  Color.RED.compareTo(Color.BLUE); // Returns the result  -1

(3) values() method: static method, return 1 array containing all enumerated values.

  Color[] colors=Color.values();
         for(Color c:colors){
         }// Return results: RED,BLUE,BLACK YELLOW,GREEN,

(4) toString() method: Returns the name of the enumeration constant.

  Color c=Color.RED;
     System.out.println(c);// Returns the result : RED

(5) valueOf() method: This method, corresponding to toString, returns an enumeration constant of the specified enumeration type with the specified name.

      Color.valueOf("BLUE");  // Returns the result : Color.BLUE

(6) equals() method: Compare the references of two enumerated class objects.

public enum Color{ 

Enumeration class can be used in switch statement.

public enum Color{ 

Thank you for reading, I hope to help you, thank you for your support to this site!

Related articles: