Solved

reference calling object

Posted on 2004-10-04
12
185 Views
Last Modified: 2010-03-31
Is there a (reasonable) way to reference the calling object without explicitly passing "this". Or in other words, does the called object know who called it?

Thanks!



0
Comment
Question by:adg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12223590
no
0
 
LVL 3

Author Comment

by:adg
ID: 12223605
Thats what I thought.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12224186
There should be a way digging in the assembly code.
But, what's the purpose?
;JOOP!
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 7

Expert Comment

by:tomboshell
ID: 12224234
Could throw and catch an exception and parse the trace, then use some reflection.
0
 
LVL 92

Expert Comment

by:objects
ID: 12224276
> Could throw and catch an exception and parse the trace

that'd only get you the method, not the object.
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 20 total points
ID: 12224338
Well, this way of working would reveal the classes.
But still not the object itself.
;JOOP!
0
 
LVL 7

Assisted Solution

by:tomboshell
tomboshell earned 30 total points
ID: 12224371
If it is simply to identify what called it then that is good.  If it is desired to invoke methods on the caller then it of course would not work that well, except for final static methods and such.  Since the trace-to-reflection route would create a new object and not the calling object, but it would be an object of the same class/type.  
That would be a kinda crazy way and due to the reflection not the best for performance, so a call back object being passed to the class is the best for such purposes.  But it could be fun! And also provide lots of entertainment by debugging and maintaining.
0
 
LVL 92

Expert Comment

by:objects
ID: 12224392
> but it would be an object of the same class/type.  

but very different state.
and how would you create it?
0
 
LVL 3

Author Comment

by:adg
ID: 12224945
Wow, I hadn't realized all these nuances. In this case, I'm just interested in identifying the class of the caller. But I know that isn't exactly what I asked (because I didn't know any better).

>> But it could be fun! And also provide lots of entertainment by debugging and maintaining
Hah, just what I need!

I'm going to raise the points and split them. Thanks for all your help!
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 12224988
try this:

StackTraceElement[] trace = new Exception().getStackTrace();
String classname = trace[trace.length-2].getClassName();
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 12225227
Thanks for the grade!

>> how would you create it?  
I would have to be a bit obtuse and say that it depends.  could do a classForName or use the class loader (hopefully it is the same) or use findClass(String name), could access a helper class (or a factory) with the string name, which I think would be preferrable.  It depends on where the class is and what constructors are available, if it is that person's class or a library, if it allows for construction outside of the package, what parameters if any, and prob other considerations that I did not mention.

>> but very different state.
I know, that is why it is not a very good way of reference, but it may be good just to get the name.  Otherwise, if it is just for knoweldge I would say to use a debugger and set a breakpoint at the part in the code that is of interest.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 12235392
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 83
JavaFX TableView not displaying correctly 3 161
Strange router problem - can't access hotmail.com 14 108
How to fix  socket closed error 11 64
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question