标签:java
编译以下类的时候,如果切换WARNING_LEVEL的赋值方式为"= StatusDef.WARNING",javac会报“非法的前向引用”。 Eclipse自带的编译器会编译通过,这是Eclipse编译器的一个缺陷。
产生这个编译错误的原因是,cinit方法是按照字段在源码中的声明顺序依次执行各个初始化代码段。 对于ForwardReference类,cinit方法依次执行以下功能:
public enum ForwardReference { C0000_1 (ForwardReference.NORMAL_LEVEL), C0000_2 (ForwardReference.WARNING_LEVEL); public static final short NORMAL_LEVEL = 0x32; // 切换以下注释行,以在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.C0000_1.getStatusLevel()); System.out.println(ForwardReference.C0000_2.getStatusLevel()); } } class StatusDef { public final static Short WARNING = 0x08; }
Javac编译时报“非法的前向引用”,以及Eclipse自带的Java编译器的缺陷
标签:java
原文地址:http://blog.csdn.net/jiangfuqiang/article/details/44464181