StackOverflow异常发生在运行Java类时。

huangapple go评论55阅读模式
英文:

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(&quot;ijshfiksh&quot;);
   }



   public static void main(String[] args) {
	  Test test = new Test();
   }
}

Error

Exception in thread &quot;main&quot; java.lang.StackOverflowError
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(Test.java:5)
at Test.&lt;init&gt;(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(&quot;ijshfiksh&quot;);
   }

   public static void main(String[] args) {
      Test test = new Test();
   }
}

Correct code:

public class Test {
    
   public Test() {

      System.out.println(&quot;ijshfiksh&quot;);
   }

   public static void main(String[] args) {
      Test test = new Test();
   }
}

huangapple
  • 本文由 发表于 2020年7月21日 18:48:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63012864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定