aspose file tools*
The moose likes Java in General and the fly likes Who called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Who called?" Watch "Who called?" New topic
Author

Who called?

Michael Labuschagne
Ranch Hand

Joined: May 08, 2007
Posts: 56

If you have two classes (Class A and Class B) with Class A having method A and class B having method B. If in method A you invoke method B is there a way that method B can find out the class which invoked the call?

i.e. how can the callee find out who the caller is without passing an extra parameters (this) from within the caller.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You can find where you were called from by examining the stack. Use Thread.getStackTrace().


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Michael Labuschagne
Ranch Hand

Joined: May 08, 2007
Posts: 56

OK... lets give the example more perspective.

Class A (has method A) and Class B(had method B)...

in method B you have a JButton which is passed to method B via the a call from method A. Within method B i want to addActionListener(instance of the calling class)

How do you get reference to the particular instance of the calling class? Thereby leaving the implementation of actionPerformed method to the bounds of Class A?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You can't do that. You'll need to pass "this".
Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
When you use ActionListener.actionPerformed(ActionEvent event), you can call event.getSource() and determine which object did generate that event.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Who called?