Java类的加载顺序:
父类静态变量-父类静态代码块-子类静态变量-子类静态代码块--父类非静态变量--父类非静态代码 块--父类构造器--子类非静态变量-子类非静态代码块--子类构造器
public class father {
public static int i = 1;
public int j = 2;
static {
System.out.println("f----"+i);
System.out.println(son.i);
}
{
System.out.println("f----"+j);
}
public father (){
System.out.println("f.con--");
}
}
public class son extends father{
public static int i = 1;
public int j = 2;
static {
System.out.println("s----"+i);
}
{
System.out.println("s----"+j);
}
public son (){
System.out.println("s.con--");
}
}
public class test {
public static void main(String[] args) {
son son = new son();
}
}
控制台:
f----1
0
s----1
f----2
f.con--
s----2
s.con--