英文:
StackOverflow exception occuring when running the java class
问题
我有一个名为Test的类。当我运行程序时,它抛出Stackoverflow错误。
类:
public class Test {
private Test test = new Test();
public Test() {
System.out.println("ijshfiksh");
}
public static void main(String[] args) {
Test test = new Test();
}
}
错误:
Exception in thread "main" java.lang.StackOverflowError
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
有人可以告诉我为什么会发生这种情况吗?
英文:
I have a class Test. When i run the program it throws Stackoverflow error.
Class:
public class Test {
private Test test = new Test();
public Test() {
System.out.println("ijshfiksh");
}
public static void main(String[] args) {
Test test = new Test();
}
}
Error
Exception in thread "main" java.lang.StackOverflowError
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
at Test.<init>(Test.java:5)
Can anyone tell my why this is happening?
答案1
得分: 2
你可以从这里看到:
public class Test {
private Test test = new Test();
...
}
你在Test
内部创建了一个Test
的实例:要构建那个Test
实例,你必须构建另一个Test
的实例,这也需要一个Test
的实例,依此类推。
英文:
As you can see from here:
public class Test {
private Test test = new Test();
...
}
You are creating an instance of Test
inside Test
: to build that Test
instance, you have to build another instance of Test
, which also requires an instance of Test
, and so on
答案2
得分: 2
当您创建一个新的Test
对象时,会调用第3行:
private Test test = new Test()
这会创建一个新的Test
对象,因此第3行会再次调用,并且这会无限循环。
StackOverflowError是Java中的运行时错误。当JVM分配的调用堆栈内存超出限制时,就会抛出此错误。这是由于无限递归创建Test
对象引起的。
英文:
When you create a new Test
object, line 3 is called:
private Test test = new Test()
This creates a new Test
object, so line 3 is called again, and this repeats forever.
A StackOverflowError is a runtime error in java. It is thrown when the amount of call stack memory allocated by JVM is exceeded. And this is caused by the infinite recursion of the creation of Test
objects.
答案3
得分: 1
public class Test {
//每次创建Test对象时,都会创建另一个Test对象,因此会导致堆栈溢出
private Test test = new Test();
public Test() {
System.out.println("ijshfiksh");
}
public static void main(String[] args) {
Test test = new Test();
}
}
修正后的代码:
public class Test {
public Test() {
System.out.println("ijshfiksh");
}
public static void main(String[] args) {
Test test = new Test();
}
}
英文:
public class Test {
//Each time you create a Test object, it creates another Test object, therefore the StackOverflow
private Test test = new Test();
public Test() {
System.out.println("ijshfiksh");
}
public static void main(String[] args) {
Test test = new Test();
}
}
Correct code:
public class Test {
public Test() {
System.out.println("ijshfiksh");
}
public static void main(String[] args) {
Test test = new Test();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论