0

There are two classes : Calculator and MenubarListener . The Calculator class has an instance of MenubarListener , and now in MenubarListener class I need to access two methods of the Calculator class and invoke them .but when I make an instance of Calculator inside MenubarListener the compiler shows stack over flow error. obviously it falls in a loop I know why it happens but I can't find a proper solution .on the other hand I don't want to make these methods static,otherwise that will create other problems.
How can I access these two methods through the second class ?

Calculator class:

public class Calculator{

    MenuBarListener menubarListener = new menubarListener(a,b,c);
            .
            .
            .
    public void method1{
     //do something     
    }
    public void method2{
     //do something 
    }
 }

MenubarListener class :

public class MenubarListener {

//when I make an instance of calculator class compiler shows stackoverflow error.
Calculator calculator = new Calculator;

int a, b,c;
MenubarListener (int a ,int b,int c ){
this.a = a ;
...   }

    @Override
    public void actionPerformed(ActionEvent e) {

    if (e.getSource() == helpItem) {
    //I want to access these methods from Calculator class but it generates
       calculator.method1();
     }
   }
 }
  • 2
    Take a step back. Clearly each class shouldn't *create* an instance of the other class, as that is an endless cycle. Instead, which class should *require* that an instance of the other class be *provided* to it? For example, suppose you have a `Window` object and a `Menu` object which goes in the Window. The Window object would *create* (and effectively own) an instance of the Menu. If the Menu needs to interact with its "parent" Window then it would require a Window reference in its constructor, which the Window object would provide when creating the Menu. – David Feb 25 '22 at 16:07

1 Answers1

1

You should NOT be creating a new instance of Calculator inside MenubarListener, just pass the reference to the Calculator via constructor/setter:

public class Calculator{

    MenuBarListener menubarListener = new MenuBarListener(this, 10, 20, 30);
//...
}
public class MenuBarListener {

    final Calculator calculator;

    int a, b, c;

    MenuBarListener (Calculator calc, int a, int b, int c) {
        this.calculator = calc;
        this.a = a;
        this.b = b;
        this.c = c;
    }
// ...
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • thank you bro . it works . but I didn't get the concept of your solution . could you give more information why we do this .? best regards – siavosh noor Feb 25 '22 at 20:33
  • 1
    It is explained in David's comment below the question: there's a cycle: create Calculator -> create MenuBarListener (MBL) -> create Calc -> create MBL ... without end. The other solution might be to follow the usual pattern of implementing the listener class as _inner_ class of `Calculator` or another UI `Window` and as such it has access to the fields / methods of the outer class. – Nowhere Man Feb 26 '22 at 02:26