• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I reference the object that invoked another object's method  RSS feed

 
sridhar satuloori
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I have seen this question in javaworld forum (http://forums.devworld.com/webx?14@120.MfiwaiOLnYP^84875@.ee6b80a/1701). There are no response to this. I am very much intrested to know the answer for this.
===============>>>>
How can I reference the class of the object that invoked a method in another class?
For example, I have the class: com.foo.stuff.PrimaryClass
It has an instance variable of type com.foo.util.WorkerClass
As some point, PrimaryClass invokes the method: workerClass.doSomething(someString);
Inside the workerClass.doSomething method, I need to invoke the getClass method on the calling Object (PrimaryClass in this case)
So inside WorkerClass I would have something like:
public void doSomething(String name)
{
����Object callingObject = ???;
����Class c = callingObject.getClass();
����...
}
What is the ???
Thanks in advance!
========================<<<<<
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's an easy answer and a hard answer.
The easy answer: (If you need the instance of the object, the only answer that I know of)

The hard answer, if all you want is the class name of the calling class and are using Java 1.4:

[ May 13, 2003: Message edited by: Joel McNary ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We ought to caution you to step back and ask yourself why you need this. Making a called class dependent on knowing the calling class can do bad things to your object model and inhibit future reuse and extension.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!