【问题标题】:Enums - static and instance blocks枚举 - 静态和实例块
【发布时间】:2012-07-10 07:05:55
【问题描述】:

我了解到,在 Java 中,静态块在类初始化时执行,而实例块在类的每个实例构造之前执行。我总是在实例块之前看到要执行的静态块。为什么enums 的情况相反?

谁能解释一下示例代码的输出:

enum CoffeeSize {

    BIG(8), LARGE(10),HUGE(12),OVERWHELMING();
    private int ounces ;

    static {
        System.out.println("static block ");
    }
    {
        System.out.println("instance block");
    }

    private CoffeeSize(int ounces){
        this.ounces = ounces;
        System.out.println(ounces);
    }
    private CoffeeSize(){
        this.ounces = 20;
        System.out.println(ounces);
    }

    public int getOunces() {
        return ounces;
    }
} 

输出:

实例块
8
实例块
10
实例块
12
实例块
20
静态块

【问题讨论】:

  • 我只是想补充一点,我会立即成为任何提供OVERWHELMING 尺寸咖啡杯的咖啡馆的常客。

标签: java enums static-block


【解决方案1】:

您需要知道枚举值是包含该枚举类型实例的静态字段,静态字段的初始化顺序取决于它们的位置。 看这个例子

class SomeClass{
    public SomeClass() { System.out.println("creating SomeClass object"); }
}

class StaticTest{
    static{ System.out.println("static block 1"); }
    static SomeClass sc = new SomeClass();
    static{ System.out.println("static block 2"); }

    public static void main(String[] args) {
        new StaticTest();
    }
}

输出

static block 1
creating SomeClass object
static block 2

现在由于枚举值总是放在枚举类型的开头,所以它们总是会在任何静态初始化块之前被调用,因为其他所有内容都只能在枚举值之后声明。
但是枚举值的初始化(发生在类初始化时)它们的构造函数被调用,正如你所说的,非静态初始化块在每个构造函数开始时执行,这就是你看到它们的原因:

  • 对于每个枚举值
  • 在任何静态初始化块之前。

【讨论】:

  • 这就是为什么你应该把它们都写成大写,这表明static final是常量,它们就是这样。
  • @Pshemo:但我无法在定义该枚举中的常量之前定义静态块。它会产生编译错误。这是否意味着对于所有枚举,将首先初始化静态最终常量,然后执行任何其他静态块?
  • @noob 在枚举的代码中,第一件事必须始终是它的元素。但并非所有static final constants will be initialized first and then any other static block 都是真的。例如,如果你在枚举元素之后放置一些静态块,在它之后放置一些静态最终元素,那么静态块将在该静态最终元素之前被调用。
【解决方案2】:

1.枚举类型是一种其字段由一组固定常量组成的类型。常见示例包括指南针方向(NORTH、SOUTH、EAST 和 WEST 的值)和星期几。

2.它们是静态最终常量,因此所有字母都大写。

3. JVM 加载类后,就会立即初始化静态变量。

更多详情请看此链接:

http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html

【讨论】:

    【解决方案3】:

    有点晚了,在Pshemo's answer 上建立起来。下面(编译)代码的输出如下:

    8
    10
    Foo
    static block 
    Bar
    

    所以首先执行枚举常量初始化(正如 Pshemo 所说,它们总是隐含的 staticfinal,参见第二个块引用),然后初始化所有显式声明为 static 的字段。如前所述,语言规范说明了类初始化期间的执行顺序和枚举常量:

    Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

    In addition to the members that an enum type E inherits from Enum, for each declared enum constant with the name n, the enum type has an implicitly declared public static final field named n of type E. These fields are considered to be declared in the same order as the corresponding enum constants, before any static fields explicitly declared in the enum type.


    class StaticTest {
        enum CoffeeSize {
            BIG(8), LARGE(10);
            private int ounces;
    
            static Foo foo = new Foo();
            static { System.out.println("static block "); }
            static Bar bar = new Bar();
    
            private CoffeeSize(int ounces){
                this.ounces = ounces;
                System.out.println(ounces);
            }
        }
        public static void main(String[] args) {
            CoffeeSize cs = CoffeeSize.LARGE;
        }
    }
    
    class Foo { public Foo() { System.out.println("Foo"); } }
    class Bar { public Bar() { System.out.println("Bar"); } }
    

    【讨论】:

      【解决方案4】:

      使用字节码来解决这个问题。

      import java.util.ArrayList;
      import java.util.List;
      
      public enum EnumDemo {
          ONE(1), TWO(2);
      
          private final static List<Integer> vals;
          static {
              System.out.println("fetch instance from static");
              vals = new ArrayList<>();
              EnumDemo[] values = EnumDemo.values();
              for (EnumDemo value : values) {
                  vals.add(value.val);
              }
          }
      
          private int val;
          EnumDemo(int val){
              this.val = val;
              System.out.println("create instance:" + val);
          }
      
      }
      

      用javac编译成class文件,然后javap -c EnumDemo.class,得到这个:

      Compiled from "EnumDemo.java"
      public final class EnumDemo extends java.lang.Enum<EnumDemo> {
        public static final EnumDemo ONE;
      
        public static final EnumDemo TWO;
      
        public static EnumDemo[] values();
          Code:
             0: getstatic     #1                  // Field $VALUES:[LEnumDemo;
             3: invokevirtual #2                  // Method "[LEnumDemo;".clone:()Ljava/lang/Object;
             6: checkcast     #3                  // class "[LEnumDemo;"
             9: areturn       
      
        public static EnumDemo valueOf(java.lang.String);
          Code:
             0: ldc_w         #4                  // class EnumDemo
             3: aload_0       
             4: invokestatic  #5                  // Method java/lang/Enum.valueOf:(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;
             7: checkcast     #4                  // class EnumDemo
            10: areturn       
      
        static {};
          Code:
             0: new           #4                  // class EnumDemo
             3: dup           
             4: ldc           #16                 // String ONE
             6: iconst_0      
             7: iconst_1      
             8: invokespecial #17                 // Method "<init>":(Ljava/lang/String;II)V
            11: putstatic     #18                 // Field ONE:LEnumDemo;
            14: new           #4                  // class EnumDemo
            17: dup           
            18: ldc           #19                 // String TWO
            20: iconst_1      
            21: iconst_2      
            22: invokespecial #17                 // Method "<init>":(Ljava/lang/String;II)V
            25: putstatic     #20                 // Field TWO:LEnumDemo;
            28: iconst_2      
            29: anewarray     #4                  // class EnumDemo
            32: dup           
            33: iconst_0      
            34: getstatic     #18                 // Field ONE:LEnumDemo;
            37: aastore       
            38: dup           
            39: iconst_1      
            40: getstatic     #20                 // Field TWO:LEnumDemo;
            43: aastore       
            44: putstatic     #1                  // Field $VALUES:[LEnumDemo;
            47: getstatic     #8                  // Field java/lang/System.out:Ljava/io/PrintStream;
            50: ldc           #21                 // String fetch instance from static
            52: invokevirtual #15                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
            55: new           #22                 // class java/util/ArrayList
            58: dup           
            59: invokespecial #23                 // Method java/util/ArrayList."<init>":()V
            62: putstatic     #24                 // Field vals:Ljava/util/List;
            65: invokestatic  #25                 // Method values:()[LEnumDemo;
            68: astore_0      
            69: aload_0       
            70: astore_1      
            71: aload_1       
            72: arraylength   
            73: istore_2      
            74: iconst_0      
            75: istore_3      
            76: iload_3       
            77: iload_2       
            78: if_icmpge     109
            81: aload_1       
            82: iload_3       
            83: aaload        
            84: astore        4
            86: getstatic     #24                 // Field vals:Ljava/util/List;
            89: aload         4
            91: getfield      #7                  // Field val:I
            94: invokestatic  #26                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
            97: invokeinterface #27,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
           102: pop           
           103: iinc          3, 1
           106: goto          76
           109: return        
      }
      

      所以,枚举实例是静态实例,并且在头部。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多