Bug 469859 - Field access of a static constant variable through enum constant does not cause enum type to be initialized
Summary: Field access of a static constant variable through enum constant does not cau...
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.4.2   Edit
Hardware: Macintosh Mac OS X
: P3 minor (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-06-10 11:46 EDT by Sotirios Delimanolis CLA
Modified: 2022-08-09 03:24 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sotirios Delimanolis CLA 2015-06-10 11:46:23 EDT
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
Comment 1 Eclipse Genie CLA 2020-08-09 08:30:53 EDT
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.
Comment 2 Eclipse Genie CLA 2022-08-08 13:15:41 EDT
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.
Comment 3 Jay Arthanareeswaran CLA 2022-08-09 03:24:25 EDT
This is still relevant and would be nice to get this fixed. Any takers?