先初始化静态变量,且只需初始化一次。
public class Test {
public static void main(String[] args) {
A a1 = new A();
B b2 = new B(1);
new A();
}
}
class A {
public A() {
super();
System.out.println("A 的构造方法...");
}
public static B b = new B(0);
}
class B {
public B(int i) {
super();
System.out.println("B 的构造方法..." + i);
}
}
B 的构造方法...0
A 的构造方法...
B 的构造方法...1
A 的构造方法...