问题描述
给出两个类,在每个类之间创建一个对象会导致StackOverflow异常.这是一个JAVA项目.
我的项目中有多个类,要使用其他类,我想我会创建另一个类的对象并使用它.
说我有Main类和GUI类.我已经在MAIN中创建了GUI对象并对其进行了初始化.同样,我已经在GUI中创建了MAIN对象并对其进行了初始化.
现在,这给了我一个Stack Overflow异常,因为构造函数的调用将深入到递归中.
我该怎么办?
我想到的一种可能的解决方案是使一类变量和方法成为STATIC.
还有其他解决方案吗?请提出建议.
Given two classes, creating an object of each class in one another results in StackOverflow Exception. It is a JAVA project btw.
There are multiple classes in my projects and for using the other classes, I thought i would create objects of the other class and use it.
Say i have class Main and class GUI. I have created object of GUI in MAIN and initialized it. Similarly i have created an object of MAIN in GUI and initialized it.
Now this gives me a Stack Overflow Exception as the the constructor calls are going deep into recursion.
How do i go about it?
One possible solution i can think of is making variables and methods of one class STATIC.
Any other solution? Please suggest.
推荐答案
这篇关于给定两个类,在每个类之间创建一个对象会导致StackOverflow Exception.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!