JDK source reading: Enum class

Article directory

Enumeration type

Define an enumeration type

public enum Calculator {

    ADD,SUBSTRACT,MUTIPLE,DIVIDE;
    
}

Enum is very similar to a special class. In fact, the type defined by enum declaration is a class These classes are all subclasses of the enum class in the class library (java.lang.Enum). They inherit many useful methods from this enum.

Compile

Compile Calculator.java into. class file through javac tool
Command: javac Calculator.java

Compile to readable bytecode file through javap

public final class huyp.learning.jdk8.lang.Calculator extends java.lang.Enum<huyp.learning.jdk8.lang.Calculator> {
  public static final huyp.learning.jdk8.lang.Calculator ADD;

  public static final huyp.learning.jdk8.lang.Calculator SUBSTRACT;

  public static final huyp.learning.jdk8.lang.Calculator MUTIPLE;

  public static final huyp.learning.jdk8.lang.Calculator DIVIDE;

  public static huyp.learning.jdk8.lang.Calculator[] values();
    Code:
       0: getstatic     #1                  // Field $VALUES:[Lhuyp/learning/jdk8/lang/Calculator;
       3: invokevirtual #2                  // Method "[Lhuyp/learning/jdk8/lang/Calculator;".clone:()Ljava/lang/Object;
       6: checkcast     #3                  // class "[Lhuyp/learning/jdk8/lang/Calculator;"
       9: areturn

  public static huyp.learning.jdk8.lang.Calculator valueOf(java.lang.String);
    Code:
       0: ldc           #4                  // class huyp/learning/jdk8/lang/Calculator
       2: aload_0
       3: invokestatic  #5                  // Method java/lang/Enum.valueOf:(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;
       6: checkcast     #4                  // class huyp/learning/jdk8/lang/Calculator
       9: areturn

  static {};
    Code:
       0: new           #4                  // class huyp/learning/jdk8/lang/Calculator
       3: dup
       4: ldc           #7                  // String ADD
       6: iconst_0
       7: invokespecial #8                  // Method "<init>":(Ljava/lang/String;I)V
      10: putstatic     #9                  // Field ADD:Lhuyp/learning/jdk8/lang/Calculator;
      13: new           #4                  // class huyp/learning/jdk8/lang/Calculator
      16: dup
      17: ldc           #10                 // String SUBSTRACT
      19: iconst_1
      20: invokespecial #8                  // Method "<init>":(Ljava/lang/String;I)V
      23: putstatic     #11                 // Field SUBSTRACT:Lhuyp/learning/jdk8/lang/Calculator;
      26: new           #4                  // class huyp/learning/jdk8/lang/Calculator
      29: dup
      30: ldc           #12                 // String MUTIPLE
      32: iconst_2
      33: invokespecial #8                  // Method "<init>":(Ljava/lang/String;I)V
      36: putstatic     #13                 // Field MUTIPLE:Lhuyp/learning/jdk8/lang/Calculator;
      39: new           #4                  // class huyp/learning/jdk8/lang/Calculator
      42: dup
      43: ldc           #14                 // String DIVIDE
      45: iconst_3
      46: invokespecial #8                  // Method "<init>":(Ljava/lang/String;I)V
      49: putstatic     #15                 // Field DIVIDE:Lhuyp/learning/jdk8/lang/Calculator;
      52: iconst_4
      53: anewarray     #4                  // class huyp/learning/jdk8/lang/Calculator
      56: dup
      57: iconst_0
      58: getstatic     #9                  // Field ADD:Lhuyp/learning/jdk8/lang/Calculator;
      61: aastore
      62: dup
      63: iconst_1
      64: getstatic     #11                 // Field SUBSTRACT:Lhuyp/learning/jdk8/lang/Calculator;
      67: aastore
      68: dup
      69: iconst_2
      70: getstatic     #13                 // Field MUTIPLE:Lhuyp/learning/jdk8/lang/Calculator;
      73: aastore
      74: dup
      75: iconst_3
      76: getstatic     #15                 // Field DIVIDE:Lhuyp/learning/jdk8/lang/Calculator;
      79: aastore
      80: putstatic     #1                  // Field $VALUES:[Lhuyp/learning/jdk8/lang/Calculator;
      83: return
}

The statement should be translated into instructions. In order to make it easier, we use the jad tool to decompile it and get the Calculator.java file

Decompile

Command: jad -sjava Calculator.class

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Calculator.java

package huyp.learning.jdk8.lang;


public final class Calculator extends Enum
{

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

    public static Calculator valueOf(String s)
    {
        return (Calculator)Enum.valueOf(huyp/learning/jdk8/lang/Calculator, s);
    }

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

    public static final Calculator ADD;
    public static final Calculator SUBSTRACT;
    public static final Calculator MUTIPLE;
    public static final Calculator DIVIDE;
    private static final Calculator $VALUES[];

    static 
    {
        ADD = new Calculator("ADD", 0);
        SUBSTRACT = new Calculator("SUBSTRACT", 1);
        MUTIPLE = new Calculator("MUTIPLE", 2);
        DIVIDE = new Calculator("DIVIDE", 3);
        $VALUES = (new Calculator[] {
            ADD, SUBSTRACT, MUTIPLE, DIVIDE
        });
    }
}

summary

According to the results of decompilation, the characteristics and usage of enum class defined by enum are introduced in detail.

1. The Calculator enumeration class is class, and it is a final class that cannot be inherited.

Its enumeration values (ADD, SUBSTRACT, MULTIPLE, DIVIDE) are static constants of type Calculator, which is also the reason why enumeration values of enumeration classes should be all uppercase

2. The enumeration class is class. Of course, there are constructors, methods and data fields in the enumeration type.

The constructor of an enumeration class is private and is used to construct values

3. Enum class inherits enum class

Override methods such as values(), valueOf(), etc

Keywords: calculator Java

Added by fitzromeo on Wed, 27 Nov 2019 22:21:00 +0200