为什么Java在枚举中提供静态块?

时间:2014-05-05 18:58:48

标签: java enums

这是我的枚举天的代码:

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

    String name;

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

这是Enum上面的Days.class文件:

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);
}

我的问题是,为什么Java提供静态块以及它的用途是什么?

2 个答案:

答案 0 :(得分:4)

使用javap -c Days,您将发现此静态块内的内容。编译器实际上编译枚举,好像它是这样编写的常规类:

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

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

    ...
}

答案 1 :(得分:1)

如果使用带有javap标志的-c来反汇编代码,它将显示静态块中发生的情况。它初始化枚举类型的枚举常量。

 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