父类静态成员变量 -> 本类静态变量 -> 父类非静态变量 -> 父类构造函数 -> 本类非静态变量 -> 本类构造函数
Demo:
public class Main {
public static void main(String[] args) {
TestClass tc = new TestClass();
}
private static class Base {
public Base(String id) {
System.out.println("Base initialized :"+id);
}
}
private static class Parent {
Base b2 = new Base("b2");
static Base b4 = new Base("b4");
public Parent() {
System.out.println("Parent initialized.");
testSync();
}
public void testSync() {
System.out.println("Parent testSync.");
}
}
private static class TestClass extends Parent{
private final static Base b = new Base("b1");
private final Base b3 = new Base("b3");
public TestClass() {
//super();
synchronized(b3) {
System.out.println("TestClass initialized.");
}
}
@Override
public void testSync() {
synchronized(b) { //crash on synchronized(b3)
System.out.println("TestClass testSync.");
}
}
}
}
输出:
Base initialized :b4
Base initialized :b1
Base initialized :b2
Parent initialized.
TestClass testSync.
Base initialized :b3
TestClass initialized.