package cn.tedu.review;
import java.security.PrivateKey;
public class MyTry {
{
a=2;
System.out.println("构造代码块"+a);//Illegal forward reference a报错
}
private int a =1;
public static void main(String[] args) {
System.out.println();
Try t= new Try(5);
MyTry m = new MyTry();
System.out.println(m.a);
m.a= t.a;
System.out.println(m.a);
}
}
class Try{
int a =1;
{
a=2;
System.out.println("构造代码块"+a);
}
//int a =1;
public Try(int a) {
this.a = a;
System.out.println("有参构造"+a);
}
}
前置引用异常
构造代码块在构造函数调用时执行,此时成员变量还没有初始化,所以报错
意思就是,输出语句执行的时候a还不存在