最近对Java中两个类交叉引用的问题有点迷惑,如下代码
类A
public class A {
private B b;
A(){
b = new B();
System.out.println("class A is building");
}
}
类B
public class B {
private A a;
B(){
a = new A();
System.out.println("class B is building");
}
}
测试类
public class Test {
public static void main(String[] args) {
A a = new A();
}
}
测试方法运行后,提示
Exception in thread "main" java.lang.StackOverflowError
请各位大哥帮忙分析分析
阿晨1998
慕后森
猛跑小猪
相关分类