在 Java 中获取 StackOverFlowError
这是我正在写的程序。当我运行它时,我收到一个 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
类作为引用时,我才会收到 StackOverflowError
s内部类。如果我使用其他类,我不会收到该错误。谁能向我澄清这一点吗?
抱歉,我非常感谢您的回答,但我有一个疑问,我不知道只有当我在同一个类的构造函数中创建实例时,才可以重复初始化。那么如何可能有多个初始化?
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 StackOverflowError
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
内部类的实现只是覆盖 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.
这一行:
创建一个新的 maininherit 对象(我将其称为
m0
),它具有相同类型的字段(我将其称为m1
)。创建后,m1 被初始化。 m1也有一个字段,所以m2被初始化。 m2 也有一个字段,因此 m3 被初始化等。
如果
StackOverflowError
没有出现,这将永远持续下去This line:
Creates a new maininherit object (I'll call it
m0
), which has a field of the same type (I'll call thatm1
)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这里的问题不在于您的
main
和mai
函数,而是在于您对maininherit h
成员变量的初始化。问题是每个 maininherit 实例都创建一个 maininherit 对象(该对象又创建一个 maininherit 对象等)。将其标记为静态成员将导致所有实例共享单个副本,这将解决此问题。The issue here is not with your
main
andmai
functions, but with your initialization of themaininherit h
member variable. The problem is that each instance ofmaininherit
is creating amaininherit
object (which in turn, creates amaininherit
object, etc.). Marking that as astatic
member will cause a single copy to be shared by all instances, which will solve this issue.每当您尝试创建同一类的实例作为成员变量时,就会出现 Stackoverflow 错误。因此 h 实例被创建无限次,因此出现了 stackoverflow 错误。
例如,您给出的代码,即使没有内部类,它也会抛出 stackoverflow 错误
避免将类本身的对象创建为成员或字段
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
Avoid creating object of the class itself as a member or field