I have a class hierarchy like this:
public class A {
private B obj = new B(); // Inside this object
// do I need a reference to here?
// (In order to call method1())
public A(){ ... }
private void method1(){ ... }
private void method2(){ ... }
}
// Other package
public class B {
private JButton bt1 = new JButton("Button");
public B(){
...
bt1.addMouseListener(new MouseActionsListener(this));
}
public class MouseActionsListener implements MouseListener
{
@Override
public void mouseClicked(MouseEvent event)
{
/*
* I need to call method1() HERE!!!
*/
}
}
}
Is it possible to call a method of the class A from the class B in that position?
The problem is that I have a list of B objects in A, and whenever a button is clicked in one of the B objects a change has to be made in A.
Thank you!
There is no implicit association that would allow you to go from
BtoA.You will need to:
Bto keep a reference to the corresponding instance ofA;B‘s constructor;A‘s methods.In code: