java 类的初始化顺序

public class FatherClass  {
	public  final int aa= getIntValuea();
	
	public FatherClass(){
		System.out.println("FatherClass construtor");
	}

	public  int getIntValuea(){
		System.out.println("FatherClass getInValue");
		return 11;
	}
}



public class SubClass extends FatherClass {
	
	
	private int inta= getIntValue();
	private int intaa= 2;
	public SubClass(){
		System.out.println("SubClass construtor");
	}
	public static void main(String[] args) {
		System.out.println("main method");
		new SubClass();
	}
	
	public int getIntValue(){
		System.out.println("getInValue");
		System.out.println(SUB);
		System.out.println(intaa);
		return 11;
	}
	public   String SUB = "str";
}



输出

main method
FatherClass getInValue
FatherClass construtor
getInValue
null
0
SubClass construtor

1、程序首先执行main主线程,打印输出main method
2、new SubClass();时先检查父类,并加载
3、加载类时,先加载static 定义的字段,然后按定义顺序初始化其他字段,最后调用构造函数。
4、最后按三的方法初始化子类

你可能感兴趣的:(java)