constant references in enums

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Fri Jan 22 00:52:17 UTC 2016


Compiler output for my last example:

Main.java:48: error: illegal reference to static field from initializer
    int field1 = e;
                 ^
Main.java:52: error: illegal reference to static field from initializer
     int i1 = e;
              ^
Main.java:57: error: illegal reference to static field from initializer
     int i1 = e;
              ^
3 errors


As you can see, static reference from instance context is only allowed 
when the static is also a constant - as per 8.9.2.

Maurizio

On 22/01/16 00:51, Maurizio Cimadamore wrote:
> My bad - the section you quote refers to a different scenario:
>
> enum Foo {
>     BAR;
>
>    static int e = 0; //non constant
>    final static int ce = 42; //constant
>
>    //8.9.2:  It is a compile-time error to reference a static field of 
> an enum type from...
>
>    // instance variable initializer expression
>    int field1 = e;
>    int field2 = ce;
>
>    // instance initializers
>    {
>     int i1 = e;
>     int i2 = ce;
>    }
>
>    //constructors
>    Foo() {
>     int i1 = e;
>     int i2 = ce;
>    }
> }
>
> So, in your case the access is occurring from another *static* 
> variable initializer, so 8.9.2 does not apply. Which I think means 
> that the same rules for ordinary classes apply - meaning that forward 
> reference for statics is only allowed with fully qualified name. So it 
> is consistent. And it is not a bug. Let's wait from some spec guru to 
> chime in.
>
> Maurizio
>
> On 22/01/16 00:41, Maurizio Cimadamore wrote:
>> If it helps - this is rejected too:
>>
>> class FooE {
>>         static FooE fe = new FooE(FOOC);  // ?? error: illegal 
>> forward reference
>>         FooE(int k) {}
>>         public static final int FOOC = 42;  // a constant variable
>>     }
>>
>> But - this isn't:
>>
>> class FooE {
>>         static FooE fe = new FooE(*FooE.*FOOC);  // ?? error: illegal 
>> forward reference
>>         FooE(int k) {}
>>         public static final int FOOC = 42;  // a constant variable
>>         // look, JLS 8.9.2 says "...unless the field is a constant 
>> variable"
>>     }
>>
>> And I know this is not a bug - i.e. the spec says that fully 
>> qualified names are allowed to forward references (see 8.3.3).
>>
>> So, I'm assuming that, since javac desugars away enums at parsing 
>> time (by translating them into classes similar to the one above), it 
>> ends up generating illegal code in your specific case. Or maybe the 
>> spec should be rectified to do something consistent in both cases.
>>
>> Btw - if you add 'FooE.' your examples compiles too :-)
>>
>> Maurizio
>>
>> On 22/01/16 00:16, John Rose wrote:
>>> enum FooE {
>>>          FOOV(FOOC);  // ?? error: illegal forward reference
>>>          FooE(int k) {}
>>>          public static final int FOOC = 42;  // a constant variable
>>>          // look, JLS 8.9.2 says "...unless the field is a constant variable"
>>>      }
>>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20160122/ccc3b803/attachment.html>


More information about the compiler-dev mailing list