在 Java 中获取 StackOverFlowError

发布于 2024-10-19 17:10:20 字数 653 浏览 7 评论 0原文

这是我正在写的程序。当我运行它时,我收到一个 StackOverFlowError 异常:

public class maininherit {

   maininherit h = new maininherit() {

        @Override
        public void mai() {
            System.out.print("inner");
        }
    };

    public static void main(String[] args){
       maininherit t=new maininherit();
       t.mai();
    }

    public void mai(){
       System.out.print("hellllll");
       h.mai();
    }
  }

这里,仅当我使用 maininherit 类作为引用时,我才会收到 StackOverflowErrors内部类。如果我使用其他类,我不会收到该错误。谁能向我澄清这一点吗?

抱歉,我非常感谢您的回答,但我有一个疑问,我不知道只有当我在同一个类的构造函数中创建实例时,才可以重复初始化。那么如何可能有多个初始化?

This is program I am writing. I am getting a StackOverFlowError exception when I run it:

public class maininherit {

   maininherit h = new maininherit() {

        @Override
        public void mai() {
            System.out.print("inner");
        }
    };

    public static void main(String[] args){
       maininherit t=new maininherit();
       t.mai();
    }

    public void mai(){
       System.out.print("hellllll");
       h.mai();
    }
  }

Here I am getting StackOverflowErrors only when I am using maininherit class as a reference in the inner class. If I am using other classes I am not getting that error. Can anyone clarify this to me?

Sorry i am thank full for your answers but i had a doubt i don't know whether reasonable or not only repetition of initializations can be possible only when i created instance in constructor of that same class know.then how it is possible to have multiple initializations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不…忘初心 2024-10-26 17:10:20

内部类的实现只是覆盖 maininherit 类的一部分。所以...
您初始化类 maininherit 然后变量 h 被初始化。 New 运算符被调用,然后...内部类再次 init maininherit 并需要设置 h 变量。

您的代码是初始化 h 变量的不定式循环。

Implementation of your inner class is just override part of maininherit class. So...
You init class maininherit then variable h were initialized. New operator were called and then... inner class init maininherit again and need to set h variable.

Your code is infinitive loop of initializations h variable.

花开柳相依 2024-10-26 17:10:20

这一行:

maininherit t=new maininherit();

创建一个新的 maininherit 对象(我将其称为 m0),它具有相同类型的字段(我将其称为 m1

。创建后,m1 被初始化。 m1也有一个字段,所以m2被初始化。 m2 也有一个字段,因此 m3 被初始化等。

如果 StackOverflowError 没有出现,这将永远持续下去

This line:

maininherit t=new maininherit();

Creates a new maininherit object (I'll call it m0), which has a field of the same type (I'll call that m1)

When m0 is created, m1 is initialized. m1 also has a field, so m2 is initialized. m2 also has a field so m3 is initialized etc.

This would go on forever if the StackOverflowError didn't oocur

長街聽風 2024-10-26 17:10:20

这里的问题不在于您的 mainmai 函数,而是在于您对 maininherit h 成员变量的初始化。问题是每个 maininherit 实例都创建一个 maininherit 对象(该对象又创建一个 maininherit 对象等)。将其标记为静态成员将导致所有实例共享单个副本,这将解决此问题。

The issue here is not with your main and mai functions, but with your initialization of the maininherit h member variable. The problem is that each instance of maininherit is creating a maininherit object (which in turn, creates a maininherit object, etc.). Marking that as a static member will cause a single copy to be shared by all instances, which will solve this issue.

荆棘i 2024-10-26 17:10:20

每当您尝试创建同一类的实例作为成员变量时,就会出现 Stackoverflow 错误。因此 h 实例被创建无限次,因此出现了 stackoverflow 错误。

例如,您给出的代码,即使没有内部类,它也会抛出 stackoverflow 错误

public class maininherit {

maininherit h=new maininherit();


public static void main(String[] args){
   maininherit t=new maininherit();
   t.mai();
 }

public void mai(){
    System.out.print("hellllll");
    h.mai();
}}

避免将类本身的对象创建为成员或字段

Stackoverflow error will occur whenever you try to create instance of same class as a member variable. So h instance is created infinite times and hence stackoverflow error came.

For e.g. the code you gave, even without inner class it will throw stackoverflow error

public class maininherit {

maininherit h=new maininherit();


public static void main(String[] args){
   maininherit t=new maininherit();
   t.mai();
 }

public void mai(){
    System.out.print("hellllll");
    h.mai();
}}

Avoid creating object of the class itself as a member or field

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文