I stumbled upon a very weird bug and I can’t explain why it happens. Imagine the following enum:
import java.awt.Color;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(MyEnum.CONSTANT1.get());
System.out.println(MyEnum.CONSTANT2.get());
}
private enum MyEnum {
CONSTANT1(staticMethod1()),
CONSTANT2(staticMethod2());
private static final Color WHY_AM_I_NULL = new Color(255, 255, 255);
private final Color color;
private MyEnum(Color color) {
this.color = color;
}
public Color get() {
return color;
}
private static Color staticMethod1() {
return new Color(100, 100, 100);
}
private static Color staticMethod2() {
return WHY_AM_I_NULL;
}
}
}
The results when you run this are:
java.awt.Color[r=100,g=100,b=100]
null
The question is, why the second one is null?
Ammendment:
If you put the WHY_AM_I_NULL in a private static class inside the enum, then it is initialized first.
The problem is that all static fields (and enum instances count as such) are initialized in their declared order (specification). So when
CONSTANT2is instantiated, the fieldWHY_AM_I_NULLis still not initialized (and thereforenull).As you can’t put the field before the enum instances, you have to find some other way of doing what you want (e.g., putting the field outside the enum class). If you tell us, what you really want to accomplish, one could make further suggestions.
Edit: If you put
WHY_AM_I_NULLin a nested class, the fields of this class will be initialized as soon as the class is first accessed (i.e., in this case during the execution ofstaticMethod2).