public class testing {
testing t=new testing();
public static void main(String args[]){
testing t1=new testing();
t1.fun();
}
void fun(){
int a=2;
int b=t.fun2(a);
System.out.println(a+" "+b);
}
int fun2(int a)
{
a=3;
return a;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么上面的代码会出现以下错误?我只是想知道原因,因为StackOverFlowError在这种情况下很难预料到错误.
Exception in thread "main" java.lang.StackOverflowError
at com.testing.<init>(testing.java:4)
at com.testing.<init>(testing.java:4)
Run Code Online (Sandbox Code Playgroud)
Jig*_*shi 13
你是递归创建的实例 testing
public class testing {
testing t=new testing();
//
}
Run Code Online (Sandbox Code Playgroud)
在创建第一个实例时,它将创建新的实例,通过testing t=new testing();该实例将再次创建新实例,依此类推
| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |