在Java中类内带static关键字属性的初始化顺序一文中,我们已经明白了带有static关键字的实例域会在加载类的时候执行所有static语句,比如实例域和静态块.那么如果对于一个有继承关系的类,并且结合static关键字,它的代码执行顺序又会是怎么样的呢?
话不多说,直接上代码:
People.java
public class People {
private String name;
private static People people = new People();
private static int n = 10;
private int age = setAge();
{
System.out.println("初始化块");
System.out.println("age = " + age);
}
static {
System.out.println("静态块");
System.out.println("n = " + n);
}
public static int setAge() {
return n++;
}
public People() {
System.out.println("默认构造函数");
System.out.println("age:" + age);
}
public People(String