简体   繁体   中英

I get a StackOverflow Error when I refrence two classes. Why?

I am sorry for the poor title, but I was not sure what my problem is called. (Please let me know in the answer if you can) I have two classes in which I am trying to interact with. Class 'Setup' has variables I need in class 'Begin', and vice versa. I have tried extending class 'Setup' in the 'Begin' class by doing:

public class Begin extends Setup {

Something to note, I do reference the 'Begin' class in the 'Setup' class in this manner:

Begin b = new Begin();

I have also tried referencing both the 'Setup' and 'Begin' class in each respective class. This did not help. I am getting the 'java.lang.StackOverflowError' error in both classes. I get the error where I refrence the other class. (Error in 'Begin': 'Setup s = new Setup();' Error in 'Setup': 'Begin b = new Begin();')

I am not aware of why I am getting this error, and I did not know what 'referencing' a class was properly called. I apologize if this is a duplicate. If it is, please link me to the page. If not, please let me know what I can do to get my program working properly again. The code and classes I mentioned in this topic are just examples. They are not the classes I am working with. I thought it may be helpful to note that I am working with JFrame. I am only extending one class to JFrame, but both classes are working with JFrame. I have created a new JFrame in the class that is not extending JFrame though. If you are not able to give an answer you think will help, I will post the code my two classes. Thanks in advance! ~Rane

EDIT: Thanks guys! I am really glad you were able to help me out with the example I gave. Thanks again!

You've got infinite recursion going on.

  • Begin extends Setup,
  • and so Begin's constructor must call Setup's constructor, and initialize it's super Setup properties
  • and Setup creates internally another Begin instance
  • which will initialize another super Setup instance
  • which creates another Begin instance
  • which will initialize another super Setup instance
  • which creates another Begin instance
  • which will initialize another super Setup instance
  • which creates another Begin instance
  • ... etc until you run out of memory

Don't do that. You need to change the entire structure of your program since a super class should have no knowledge of its child classes much less instances of it. My bet is that Begin should not extend Setup, and that's where I'd start. In fact this situation:

Class 'Setup' has variables I need in class 'Begin', and vice versa.

is not solved by inheritance but by composition. For example...

public class Begin {
  private Setup setup;

  public void setSetup(Setup setup) {
     this.setup = setup;
  }

and maybe also

public class Setup {
  private Begin begin;

  public void setBegin(Begin begin) {
    this.begin = begin;
  }

Then elsewhere:

Begin begin = new Begin();
Setup setup = new Setup();

begin.setSetup(setup);
setup.setBegin(begin);

There are other ways of injecting dependency and the specifics will depend on your need, so don't take this as gospel, other than don't use inheritance for this purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM