【发布时间】:2016-08-16 03:31:06
【问题描述】:
这是我的代码:
public class ConstructorsDemo{
public static void main(String a[]){
Cons1 c1 = new Cons1();
}
}
class Cons1{
Cons1 c = new Cons1();// the error is in this line
Cons1(){
//does somwthing
}
}
所以我在这里得到一个无限循环错误(Stackoverflow)。 但是,如果我注释掉我创建的两个对象中的任何一个,就可以了。
我的代码中的对象 c 是如何导致 Stackoverflow 错误的?
【问题讨论】:
-
你可以在你的调试器中单步调试代码,看看为什么你的代码是递归的。
标签: java object constructor stack-overflow