Community
Participate
Working Groups
Hi, When the example given bellow is compiled with the Java compiler from the Eclipse 3.0 M2 then the static members in MyEnum class do not get initialized i.e. the last line prints "null". When compiled with the standard java compiler from JDK (tried 1.3.7, 1.4.0 and 1.4.2) then everything works as expected i.e. the last line prints out "A". Compilers in older versions of Eclipse also work as expected. Example: import java.util.HashMap; import java.util.Map; class Enum { private String name; private static Map nameToInstance = new HashMap(); protected Enum(String name) { nameToInstance.put(name, this); this.name = name; } public static Enum getFromName(String name) { return (Enum) nameToInstance.get(name); } public String toString() { return name; } } class MyEnum extends Enum { private MyEnum(String name) { super(name); } public static MyEnum A = new MyEnum("A"); } public class TestStaticMembers { public static void main(String[] args) throws ClassNotFoundException { Class c = MyEnum.class; // now MyEnum class should be loaded and static members initialized System.out.println(c.getName()); Enum e = Enum.getFromName("A"); System.out.println(e); // should print A } }
This is not a bug. .class is not enough to initialize the class. The class literal is not listed in the JLS as a case that causes a class initialization. I will find the appropriate PR.
Have a look at bug 37565. It explains the problem. We follow the JLS. Right now javac and the JLS are not consistent. You can try with jikes and you will get the same problem.
Ok to close? This will be revisited if the JLS are changed to add the .class as a case of class initialization.
*** This bug has been marked as a duplicate of 37565 ***