Question

This is the code of my enum Days:

enum Days {
    MON("Monday"),
    TUE("Thuesday"), 
    WED("wedesday"), 
    THU("Thursday"), 
    FRI("Firday"), 
    SAT("saturday"), 
    SUN("Sunday");

    String name;

    Days(String a) {
        name=a;
    }
}

This is the Days.class file of above Enum:

final class p1.Days extends java.lang.Enum<p1.Days> {
    public static final p1.Days MON;
    public static final p1.Days TUE;
    public static final p1.Days WED;
    public static final p1.Days THU;
    public static final p1.Days FRI;
    public static final p1.Days SAT;
    public static final p1.Days SUN;

    static {};

    public static p1.Days[] values();
    public static p1.Days valueOf(java.lang.String);
}

My question is that why does Java provide a static block and what is the use of it?

Était-ce utile?

La solution

Use javap -c Days, and you'll discover what is inside this static block. The compiler actually compiles the enum as if it was a regular class written like this:

public final class Days extends Enum<Days> {
    public static final Days MON;
    ...

    static {
        Days.MON = new Days("Monday");
        ...
    }

    ...
}

Autres conseils

If you use the javap with the -c flag to disassemble the code, it will show you what is happening in the static block. It initializes the enum constants of your enum type.

 static {};
   Code:
      0: new           #1                  // class test/Days
      3: dup
      4: ldc           #20                 // String MON
      6: iconst_0
      7: ldc           #21                 // String Monday
      9: invokespecial #23                 // Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
     12: putstatic     #27                 // Field MON:Ltest/Days;
     15: new           #1                  // class test/Days
     18: dup
     19: ldc           #29                 // String TUE
     21: iconst_1
     22: ldc           #30                 // String Thuesday
     24: invokespecial #23                 // Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
     27: putstatic     #32                 // Field TUE:Ltest/Days;
     30: new           #1                  // class test/Days
     33: dup
     34: ldc           #34                 // String WED
     36: iconst_2
     37: ldc           #35                 // String wedesday
     39: invokespecial #23                 // Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
     42: putstatic     #37                 // Field WED:Ltest/Days;
     45: new           #1                  // class test/Days
     48: dup
     49: ldc           #39                 // String THU
     51: iconst_3
     52: ldc           #40                 // String Thursday
     54: invokespecial #23                 // Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
     57: putstatic     #42                 // Field THU:Ltest/Days;
     60: new           #1                  // class test/Days
     63: dup
     64: ldc           #44                 // String FRI
     66: iconst_4
     67: ldc           #45                 // String Firday
     69: invokespecial #23                 // Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
     72: putstatic     #47                 // Field FRI:Ltest/Days;
     75: new           #1                  // class test/Days
     78: dup
     79: ldc           #49                 // String SAT
     81: iconst_5
     82: ldc           #50                 // String saturday
     84: invokespecial #23                 // Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
     87: putstatic     #52                 // Field SAT:Ltest/Days;
     90: new           #1                  // class test/Days
     93: dup
     94: ldc           #54                 // String SUN
     96: bipush        6
     98: ldc           #55                 // String Sunday
    100: invokespecial #23                 // Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
    103: putstatic     #57                 // Field SUN:Ltest/Days;
    106: bipush        7
    108: anewarray     #1                  // class test/Days
    111: dup
    112: iconst_0
    113: getstatic     #27                 // Field MON:Ltest/Days;
    116: aastore
    117: dup
    118: iconst_1
    119: getstatic     #32                 // Field TUE:Ltest/Days;
    122: aastore
    123: dup
    124: iconst_2
    125: getstatic     #37                 // Field WED:Ltest/Days;
    128: aastore
    129: dup
    130: iconst_3
    131: getstatic     #42                 // Field THU:Ltest/Days;
    134: aastore
    135: dup
    136: iconst_4
    137: getstatic     #47                 // Field FRI:Ltest/Days;
    140: aastore
    141: dup
    142: iconst_5
    143: getstatic     #52                 // Field SAT:Ltest/Days;
    146: aastore
    147: dup
    148: bipush        6
    150: getstatic     #57                 // Field SUN:Ltest/Days;
    153: aastore
    154: putstatic     #59                 // Field ENUM$VALUES:[Ltest/Days;
    157: return
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top