3

在 Java 枚举类中,我想创建一个final static包含该类的数组values()。当我按照以下几行执行此操作时,生成的数组是null.

public enum Name {
    E1( stuff ), E2( stuff );
    private static final Name[] values = Name.values();

    private Name( stuff ) { more stuff; }
}

我也尝试通过调用显式类设置器方法来执行此操作,但这给出了java.lang.ExceptionInInitializerError异常。

我知道问题是由一些浅层依赖引起的stuff,因为前面的代码使用了其他类,这些类本身依赖于枚举类。

是否有经过测试和验证的技术来实现我的需求?

4

2 回答 2

3

tl; dr:您尝试做的事情是不可能的 - 在所有构造函数调用完成之前,枚举类型的静态字段不会被初始化。


考虑这个例子:

public enum Name {
  E1("hello"), E2("world");

  private static final Name[] values = values();

  private Name(String val) {
    System.out.println("val = " + val);
    dump();
  }

  protected void dump() {
    System.out.println("this = " + this + ", values = " + values);
  }
}

请注意,该dump方法存在的原因是尝试valueName. 使用此测试工具:

public class Main {
  public static void main(String... args) throws Exception {
    System.out.println(Name.values());
  }
}

我们得到

$ java Main
val = hello
this = E1, values = null
val = world
this = E2, values = null
[LName;@35960f05

使用反编译Name类,javap我们看到以下内容:

private static final Name[] $VALUES;

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

编译器创建一个$VALUES保存值数组的私有字段,该values()方法实现为{ return (Name[])$VALUES.clone() }. 那么如何$VALUES初始化呢?

static {};
  Code:
   0:   new #4; //class Name
   3:   dup
   4:   ldc #19; //String E1
   6:   iconst_0
   7:   ldc #20; //String hello
   9:   invokespecial   #21; //Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
   12:  putstatic   #22; //Field E1:LName;
   15:  new #4; //class Name
   18:  dup
   19:  ldc #23; //String E2
   21:  iconst_1
   22:  ldc #24; //String world
   24:  invokespecial   #21; //Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
   27:  putstatic   #25; //Field E2:LName;
   30:  iconst_2
   31:  anewarray   #4; //class Name
   34:  dup
   35:  iconst_0
   36:  getstatic   #22; //Field E1:LName;
   39:  aastore
   40:  dup
   41:  iconst_1
   42:  getstatic   #25; //Field E2:LName;
   45:  aastore
   46:  putstatic   #1; //Field $VALUES:[LName;
   49:  invokestatic    #26; //Method values:()[LName;
   52:  putstatic   #18; //Field values:[LName;
   55:  return

}

我们在这里看到的是初始化本质上是:

// compiler-generated initialization code
E1 = new Name("hello");
E2 = new Name("world");
$VALUES = new Name[] {E1, E2};

// static initializer of the values field
values = Name.values();

因此在执行构造函数调用期间,该values字段将为 null,并且该values()方法将抛出 NullPointerException(它将被包装在 ExceptionInInitializerError 中)。

于 2012-09-16T15:32:05.430 回答
2

您能否提供一个发生这种情况的示例,因为它不应该为空。

public class Main {
    public enum Name {
        E1(  ), E2(  );
        private static final Name[] VALUES = Name.values();
    }


    public static void main(String... args) {
        System.out.println(Name.VALUES);
        System.out.println(Arrays.asList(Name.VALUES));
    }
}

印刷

[LMain$Name;@717e5fde
[E1, E2]
于 2012-09-16T14:15:46.350 回答