When compiling the following class, if the switch Warning_level is assigned as "= Statusdef.warning", Javac will quote "illegal forward reference". The compiler that comes with eclipse compiles through, which is a flaw in the Eclipse compiler.
The reason for this compilation error is that the Cinit method executes each initialization snippet sequentially, in the order in which the fields are declared in the source code. For the Forwardreference class, the Cinit method performs the following functions in turn:
- Create a c0000_1 field with normal_level values. In the Forwardreference.class file, the initial value of the Normal_level field is written in the attribute property of the field, so before entering the Cinit method, the Normal_level field has been set to the initial value, which is 0x32 .
- Create a c0000_2 field with warning_level values. However, the value of the Warning_level field at this point is still 0.
- Set the value of the Warning_level field to statusdef.warning. As you can see, the c0000_2 created in the second step does not contain the results expected by the developer.
public enum Forwardreference {c0000_1 (forwardreference.normal_level), c0000_2 (Forwardreference.warning_level); public static final Short normal_level = 0x32;//Toggles the following comment lines to generate an "illegal forward reference error" under Javac//public static final Short warning_level = Statusdef.warning;public static final Short warning_level = 0x08;private final short statuslevel;public short Getstatusle Vel () {return statuslevel;} Private Forwardreference (short statuslevel) {this.statuslevel = Statuslevel;} public static void Main (string[] args) {System.out.println (ForwardReference.C0000_1.getStatusLevel ()); System.out.println (ForwardReference.C0000_2.getStatusLevel ());}} Class Statusdef {Public final static short WARNING = 0x08;}
Javac compile times "illegal forward references" and flaws in the Java compiler that comes with eclipse