constant references in enums

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Fri Jan 22 00:41:47 UTC 2016


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/f3d29e61/attachment-0001.html>


More information about the compiler-dev mailing list