This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Caller reference?

 
Tom Elix
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From a method in another object, how do I access the reference to the object who called me? In other words, how do I find the object which called this object?
 
Chloe Loh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would pass the reference of the caller object into the mentioned method of the object that will be called. The declaration of the method in the class of the called object can be:
e.g. void calledMethod(Object calledObject)
I don't know if there is any more efficient way of doing it. but, if anyone have a better solution, I also want to learn it..
 
Chloe Loh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I made a mistake.
The declaration of the method in the class of the called object can be:
e.g. void calledMethod(Object callerObject)
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want this information for debugging purpose, you can also get it while writing this dirty code:

You will see all the stack, thus the caller .
W.
[ November 28, 2002: Message edited by: Wilfried LAURENT ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic