Java – how to reference the object that created the object?
•
Java
In Java, I have an object to create a button In the onclicklistener of the button, I want to reference the object that created the button
Is there any simple way to do this?
Solution
It depends on how you build it Typically, instances have no reference to the instance that created them unless you pass them in and store them somewhere But if you do this:
public class YourClass { public void foo() { JButton b = new JButton(); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Need reference to YourClass here } }); } }
Then you can use yourclass This to reference the external yourclass
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码