先说结论
变量初始化 -> 父类构造器 -> 子类构造器
贴代码
Animcal.java 父类
public class Animal
{
private static int index = 0;
private static String getStaticName()
{
String name = String.format("animal %d" , index++);
System.out.println(name);
return name;
}
private String name = getStaticName();
public Animal()
{
System.out.println("create animal");
}
}
Cat.java 子类
public class Cat extends Animal
{
public Cat()
{
System.out.println("create cat");
}
}
Main.java
public class Main {
public static void main(String[] args) throws Exception
{
Cat cat = new Cat();
}
}
打印结果
animal 0
create animal
create cat