Use and Analysis of Enum

Example:

package test;

public enum ColorEnum {
    RED,
    YELLOW,
    BLUE,
    GREEN,
    BLACK,;
}

Obviously, enum is very similar to a special class, in fact, the type defined by the enum declaration is a class.These classes are subclasses of the Enum class in the class library (java.lang.Enum<E>).They inherit many useful methods from this Enum.After compiling the code, we found that the compiler compiled the enum type separately into a byte code file: Color.class.

package test;


public final class ColorEnum extends Enum
{

    public static ColorEnum[] values()
    {
        return (ColorEnum[])$VALUES.clone();
    }

    public static ColorEnum valueOf(String s)
    {
        return (ColorEnum)Enum.valueOf(test/ColorEnum, s);
    }

    private ColorEnum(String s, int i)
    {
        super(s, i);
    }

    public static final ColorEnum RED;
    public static final ColorEnum YELLOW;
    public static final ColorEnum BLUE;
    public static final ColorEnum GREEN;
    public static final ColorEnum BLACK;
    private static final ColorEnum $VALUES[];

    static 
    {
        RED = new ColorEnum("RED", 0);
        YELLOW = new ColorEnum("YELLOW", 1);
        BLUE = new ColorEnum("BLUE", 2);
        GREEN = new ColorEnum("GREEN", 3);
        BLACK = new ColorEnum("BLACK", 4);
        $VALUES = (new ColorEnum[] {
            RED, YELLOW, BLUE, GREEN, BLACK
        });
    }
}

The following section details the characteristics and usage of enum classes defined by enum.(followed by Color examples)

1. The Color enumeration class is a class and is a final class that cannot be inherited.Its enumeration values (RED,BLUE...) are class static constants of the Color type. We can get an instance of the Color enumeration class by:
                                                         Color c=Color.RED; 
Note: These enumerated values are public static final, which is the way we often define constants, so it is best to capitalize all the enumerated values in an enumerated class.(

2. Enumeration classes are classes. Of course, there are constructors, methods, and data domains in enumeration types.However, the constructors of enumeration classes are very different:
(1) The constructor is only called when an enumeration value is constructed.

 1 package test;
 2 
 3 public enum ColorEnum {
 4     RED(255, 0, 0), BLUE(0, 0, 255), BLACK(0, 0, 0), YELLOW(255, 255, 0), GREEN(0, 255, 0);
 5 
 6     // Construct an enumeration value, such as RED(255,0,0)
 7     private ColorEnum(int rv, int gv, int bv) {
 8         this.redValue = rv;
 9         this.greenValue = gv;
10         this.blueValue = bv;
11     }
12 
13     public String toString() { // Override parent class Enum Of toString()
14         return super.toString() + "(" + redValue + "," + greenValue + "," + blueValue + ")";
15     }
16 
17     private int redValue; // Custom data fields, private For encapsulation.
18     private int greenValue;
19     private int blueValue;
20 }

(2) Constructors can only be private, and public constructors are never allowed.This ensures that external code cannot construct new instances of enumeration classes.This is also perfectly reasonable because we know that the enumeration value is a constant of public static final.However, methods and data domains of enumerated classes 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);  // Called toString()Method  
} 

3. All enumeration classes inherit Enum's methods, which are detailed below.(
(1) ordinal() method: Returns the order in which the enumeration values are enumerated.This order depends on the order in which the enumeration values are declared.
ColorEnum.RED.ordinal(); //Return result:0
ColorEnum.BLUE.ordinal(); //Return results:1
(2) compareTo() method: Enum implements the java.lang.Comparable interface, so it can compare the order of objects to the specified objects.CompeTo in Enum returns the difference between the order of the two enumerated values.Of course, if the two enumeration values must belong to the same enumeration class, the ClassCastException() exception will be thrown.(specific visible source code)
ColorEnum.RED.compareTo(Color.BLUE); //Return result-1
(3) values() method: Static method that returns an array containing all the enumerated values.
                 ColorEnum[] colors=ColorEnum.values();
                 for(ColorEnumc:colors){
                        System.out.print(c+","); 
}//Return results: RED,BLUE,BLACK YELLOW,GREEN,
(4) toString() method: Returns the name of an enumeration constant.
                 ColorEnum c=ColorEnum.RED;
System.out.println(c); //Return result: RED
(5) valueOf() method: This method corresponds to the toString method and returns an enumeration constant of the specified enumeration type with the specified name.
ColorEnum.valueOf("BLUE"); //Return result: ColorEnum.BLUE
(6) Equas() method: Compare references to two enumerated class objects.

//JDK Source code:      
public final boolean equals(Object other) {  
        return this==other;  
}               

4. Enumeration classes can be used in switch statements.

ColorEnum color = ColorEnum .RED;  
switch(color){  
        case RED: System.out.println("it's red");break;  
        case BLUE: System.out.println("it's blue");break;  
        case BLACK: System.out.println("it's blue");break;  
}  

Keywords: Java JDK

Added by ikebaldo on Sun, 07 Jul 2019 19:12:58 +0300