Javac compilation times "invalid forward reference" and the defects of the Java compiler that comes with Eclipse
When compiling the following classes, if you change the value assignment method of WARNING_LEVEL to "= StatusDef. WARNING", javac reports "invalid forward reference ". The compiler that comes with Eclipse is compiled, which is a defect of the Eclipse compiler.
The cause of this compilation error is that the cinit method executes each initialization code segment in sequence according to the declared order of the field in the source code. For the ForwardReference class, the cinit method executes the following functions in sequence:
Create the c1__1 field using the value of NORMAL_LEVEL. In ForwardReference. in the class file, the initial value of the NORMAL_LEVEL field is written in the attribute of the field. Therefore, before entering the cinit method, the NORMAL_LEVEL field has been set with the initial value 0x32. create the C0000_2 field using the value of WARNING_LEVEL. However, the value of the WARNING_LEVEL field is still 0. Set the value of the WARNING_LEVEL field to StatusDef. WARNING. As you can see, the C0000_2 created in step 2 does not contain the expected results of the developer.
Public enum ForwardReference {C0000_1 (ForwardReference. NORMAL_LEVEL), C0000_2 (ForwardReference. WARNING_LEVEL); public static final short NORMAL_LEVEL = 0x32; // switch the following annotation lines, in this case, "invalid forward reference error" is generated in javac. // public static final short WARNING_LEVEL = StatusDef. WARNING; public static final short WARNING_LEVEL = 0x08; private final short statusLevel; public short getStatusLevel () {return statusLevel;} private ForwardReference (short statusLevel) {this. statusLevel = statusLevel;} public static void main (String [] args) {System. out. println (ForwardReference. c1__1.getstatuslevel (); System. out. println (ForwardReference. c0000_2.getStatusLevel () ;}} class StatusDef {public final static Short WARNING = 0x08 ;}