Implementation principle of Java enumeration

Definition

A collection of all the values of a thing.

Properties of enumeration type

   because enumeration type represents a collection of all values of a thing, the attributes that enumeration type should have are:

  1. Enumeration types have immutable values.

The function of enumeration

                   .

The principle of enumeration

   by using the decompile command javap, we can get some understanding:

  1. The compiler compiles enumeration types into ordinary class classes.
  2. Each enumeration value in the enumeration is compiled into the form of "public static final type enumeration value".
  3. After the compiler compiles, it adds a static block in which a new object is created and assigned to the static object in point 2.

The results of decompilation with javap-c are as follows:

public final class Season extends java.lang.Enum<Season> {
  public static final Season SPIRNG;

  public static final Season SUMMER;

  public static final Season AUTUMN;

  public static final Season WINTER;

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

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

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

Source code:

public enum Season {
    SPIRNG,
    SUMMER,
    AUTUMN,
    WINTER;
}

Tags: Java

Posted on Sat, 09 Nov 2019 13:04:38 -0500 by jheitz