加载顺序为
父类静态定义初始化
子类静态定义初始化
父类定义初始化
父类构造方法
子类定义初始化
子类构造方法
package xu.jin;
class Insect{
private int i=print("hello i");
Insect(){System.out.println("Insect"+i);}
{
System.out.println("1");
System.out.println("2");
}
static{
System.out.println("3");
System.out.println("4");
}
static int print(String str){
System.out.println(str);
return 2;
}
static int x1=print("static Insect");
}
class hello extends Insect{
private int j=print(" hello j");
static int x2=print("static hello");
hello(){
System.out.println("hello"+j);
}
{
System.out.println("11");
System.out.println("22");
}
static{
System.out.println("33");
System.out.println("44");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
hello sCat=new hello();
//Insect ins=new Insect();
System.out.println("hello world");
}
}
输出为:
3
4
static Insect
static hello
33
44
hello i
1
2
Insect2
hello j
11
22
hello2
hello world