Create objects without causing a stack overflow error?

淺唱寂寞╮ 提交于 2019-11-28 14:28:33

Your Main class is creating a Secondary instance, which is creating a Main instance..., and this is causing the stack overflow error.

I think you just want the objects to refer to each other, so don't create the other class's new instance in the constructor. Declare references as instance variables, and use setter methods to store existing references to objects of the other type.

You could use the builder pattern to handle safe (non-recursive) initialization of your instance fields (via setters).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!