Community
Participate
Working Groups
The following enum declares a static field, and then accesses it from a non-static initializer. "javac" balks at this construct, and running an application from within Eclipse that uses the enumeration throws a weird Exception. ************************************* public enum ErrorEnum { UNKNOWN (); private static String error; { error = "error"; } } ***************************** javac error is: ErrorEnum.java:10: illegal reference to static field from initializer *******************************
Unclear why this should be any different from: public class X { static String other; static { other = "other"; } } which is accepted just fine.
I also looked at it, baffled, for quite some time. It might be something specific to the way enums are spec'ed (maybe the fine print in the language specification). Rather than dive into it myself, I trusted that if a) javac balks at it, and b) the java virtual machine throws a nasty error, then it's quite likely invalid... Zorzella
What nasty error did you get running our generated classfile ? I did not get any.
Tried with latest. No error reported. javac complains, but I could not understand why.
Actually, javac is right here. One cannot refer to an enum constant within code which is going to perform while it is getting initialized (enum constants are triggering constructors/initializers). Added GenericTypeTest#test791 *** This bug has been marked as a duplicate of 101713 ***
Actually, regression test is: EnumTest#test121