Community
Participate
Working Groups
When you access a constant variable class field through an enum constant, the corresponding enum type is not initialized. Take this code: public class Example { public static void main(String[] args) throws Exception { Object p = EnumType.ENUM_CONSTANT.CONSTANT_VARIABLE; } } enum EnumType { ENUM_CONSTANT; EnumType() { System.out.println("initialized"); } public static final String CONSTANT_VARIABLE = "a value"; } When you run it, nothing is printed to standard out. I expect "initialized" to be printed since we're accessing "ENUM_CONSTANT" which should trigger initialization of EnumType. In this case, the compiler seems to remove the access altogether since we're accessing a static field. According to the JLS (http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.11.1), concerning field access expressions > If the field is static: (a) The Primary expression is evaluated, and the result is discarded. [...] The enum constant should be used, triggering initialization, and discarded. The Oracle compiler pushes a reference to the constant on the stack, then pops it. It then pushes the value of the constant on the stack so it can be stored in the local variable. I've reproduced this only on Version: Luna Service Release 2 (4.4.2) Build id: 20150219-0600
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant. -- The automated Eclipse Genie.
This is still relevant and would be nice to get this fixed. Any takers?