Bug 101713 - [1.5][compiler] Access to static fields within enum constructors inconsistent with javac
Summary: [1.5][compiler] Access to static fields within enum constructors inconsistent...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1.1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 92165 102265 108905 110403 149042 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-06-24 15:35 EDT by Jon Skeet CLA
Modified: 2006-06-28 13:08 EDT (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jon Skeet CLA 2005-06-24 15:35:57 EDT
(3.1RC3)

javac forbids accessing static fields within a constructor. The class below
fails to compile with the error: "Test.java:9: illegal reference to static field
from initializer". Eclipse compiles it just fine.

It's a reasonable thing to at least warn about, as any static initializer on the
field won't execute until after all the enum values have been constructed. It's
also a bit of a pain when everything works in Eclipse and then javac breaks :(

Here's the code:

public enum Test
{
    SomeValue;
	
    static int asd;
    
    private Test()
    {
        asd=5;
    }
}
Comment 1 Olivier Thomann CLA 2005-06-24 17:13:47 EDT
Indeed this seems to be a bug in Eclipse compiler.
From the JLS (page 257),
It is a compile-time error to reference a non-constant (ยง15.28) static field of
an enum type from its constructors, instance initializer blocks, or instance
variable
initializer expressions.
Comment 2 Philipe Mulet CLA 2005-06-28 08:05:03 EDT
Only implicit references are forbidden, following would be fine:

public enum Test
{
    SomeValue;
	
    static int asd;
    
    private Test()
    {
        Test.asd=5;
    }
}
Comment 3 Jon Skeet CLA 2005-06-28 12:28:18 EDT
Aha - that's good to know, thanks :)
Comment 4 Philipe Mulet CLA 2005-06-28 15:13:12 EDT
Note that qualified references may yield default values for fields, it simply
bypasses the normal initialization.

e.g. 
class X {
  static int FOO = X.BAR;
  static int BAR = 2;
}
--> FOO will be 0.
Comment 5 Philipe Mulet CLA 2005-06-28 15:13:53 EDT
Clarifying: it actually bypasses the check for initialization.
Comment 6 Philipe Mulet CLA 2005-06-28 15:15:07 EDT
Added EnumTest#test115-116.
Fixed. Will be released to 3.1 maintenance branch as soon as it is created.
Comment 7 Philipe Mulet CLA 2005-06-28 15:16:45 EDT
Fix is in Scope#getBinding(char[]...), it ensures that when finding a static
field in enum, it isn't located inside offending context.
Comment 8 Philipe Mulet CLA 2005-06-29 04:11:04 EDT
Fixed in 3.1 maintenance branch
Comment 9 Philipe Mulet CLA 2005-06-30 03:52:09 EDT
*** Bug 102265 has been marked as a duplicate of this bug. ***
Comment 10 Philipe Mulet CLA 2005-07-19 12:15:57 EDT
*** Bug 92165 has been marked as a duplicate of this bug. ***
Comment 11 Maxime Daniel CLA 2005-08-09 09:57:12 EDT
Verified in 3.2 M1 with build I20050808-2000.
Comment 12 Philipe Mulet CLA 2005-09-07 09:41:48 EDT
*** Bug 108905 has been marked as a duplicate of this bug. ***
Comment 13 Olivier Thomann CLA 2005-09-25 10:28:46 EDT
*** Bug 110403 has been marked as a duplicate of this bug. ***
Comment 14 David Audel CLA 2005-09-26 10:03:12 EDT
Verified using M20050923-1430 for 3.1.1
Comment 15 Olivier Thomann CLA 2006-06-28 13:08:03 EDT
*** Bug 149042 has been marked as a duplicate of this bug. ***