在线程“main”中获得异常java.lang.StackOverflowError的 [英] Getting exception in thread "main" java.lang.StackOverflowError

查看:401
本文介绍了在线程“main”中获得异常java.lang.StackOverflowError的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java和OOP的新手,这是我的问题。当我运行下面的代码时,我得到


线程main中的异常java.lang.StackOverflowError。


我在代码中创建了一个 JavaApplication1 的对象。问题不在于class App2 。如果在run方法中创建了对象 ja ,则代码可以正常工作。你能解释一下为什么吗?

  package javaapplication1; 

公共类JavaApplication1 {

int i,k,j;

class App2 {
int i = 23;
int j = 12;
}

App2 a2 = new App2();
JavaApplication1 ja = new JavaApplication1();

public void run(){
ja.i = 10;
a2.i = 26;
a2.j = 18;
System.out.println(i +,+ j +'+ ja.i +'
+ a2.i +'+ a2.j +'+ K);
}

public static void main(String [] args){
int k = 24;
JavaApplication1 ja1 = new JavaApplication1();
ja1.run();
ja1.i = 18;
System.out.println(ja1.i +'+'+ k);
}
}


解决方案

您的class JavaApplication1 有字段 JavaApplication1 ja ,其中包含另一个 JavaApplication1 class,它还有自己的 ja 字段,该字段包含另一个 JavaApplication1 的实例,依此类推。 / p>

换句话说,当你创建 JavaApplication1 的实例时,这个实例会创建它的内部实例 JavaApplication1 并且此内部实例创建另一个 JavaApplication1 实例,该实例再次创建实例 JavaApplication1 ..直到堆栈已满。



因此,当您在main方法中运行此代码时

  JavaApplication1 ja1 = new JavaApplication1(); 

类似这样的事情

  + ------------------------------------------ ----- + 
ja1 - > | JavaApplication1实例|
+ --------------------------------------------- - +
| |
| + ------------------------------------ + |
| ja - > | JavaApplication1实例| |
| + ------------------------------------ + |
| | | |
| | + ------------------------- + | |
| | ja - > | JavaApplication1实例| | |
| | + ------------------------- | | |
| | | | | |
| | | ja - > .... | | |
| | + ------------------------- + | |
| + ------------------------------------ + |
+ --------------------------------------------- - +

无论如何我看不到 ja 字段已被使用,因此请考虑从代码中删除它。


I am new to Java and OOPs and here is my issue. When I run the below code, I get the

Exception in thread "main" java.lang.StackOverflowError.

The problem is in the code were I create an object of JavaApplication1.The problem is not occurring for class App2. The code works fine if the object ja is created inside the run method. Can you explain me why?

package javaapplication1;

public class JavaApplication1 {

    int i, k, j;

    class App2 {
        int i = 23;
        int j = 12;
    }

    App2 a2 = new App2();
    JavaApplication1 ja = new JavaApplication1();

    public void run() {
        ja.i = 10;
        a2.i = 26;
        a2.j = 18;
        System.out.println(i + "," + j + "'" + ja.i + "'"
                           + a2.i + "'" + a2.j + "'" + k);
    }

    public static void main(String[] args) {
        int k = 24;
        JavaApplication1 ja1 = new JavaApplication1();
        ja1.run();
        ja1.i = 18;
        System.out.println(ja1.i + "'" + "'" + k);
    }
}

解决方案

Your class JavaApplication1 has field JavaApplication1 ja which holds another instance of JavaApplication1 class, which also has its own ja field which holds another instance of JavaApplication1, and so on and on.

In other words, when you create instance of JavaApplication1 this instance creates its inner instance of JavaApplication1 and this inner instance creates another JavaApplication1 instance, which again creates instance JavaApplication1... until stack will be full.

So when you run this code in your main method

JavaApplication1 ja1 = new JavaApplication1();

something like this happens

       +-----------------------------------------------+
ja1 -> | JavaApplication1 instance                     |
       +-----------------------------------------------+
       |                                               |
       |       +------------------------------------+  |
       | ja -> | JavaApplication1 instance          |  |
       |       +------------------------------------+  |
       |       |                                    |  |
       |       |       +-------------------------+  |  |
       |       | ja -> |JavaApplication1 instance|  |  |
       |       |       +-------------------------|  |  |
       |       |       |                         |  |  |
       |       |       | ja -> ....              |  |  |
       |       |       +-------------------------+  |  |
       |       +------------------------------------+  |
       +-----------------------------------------------+

Anyway I don't see where ja field is ever used, so consider removing it from your code.

这篇关于在线程“main”中获得异常java.lang.StackOverflowError的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆