getting the caller name.

Hi All.

I waned to know if thre are any ways I can know the caller class/object type from with in a code.

I mean if there is a method foo() inside, a class A.

if there are 2 classes B and C extend A; can I write few lines of code inside A.foo() to know that the invoker of method foo is an instance of class B or C?

Any pointers will be helpful.

Rgds

[387 byte] By [ayusman_dikshita] at [2007-10-3 5:17:08]
# 1
System.out.println(getClass().getName());
jfbrierea at 2007-7-14 23:23:49 > top of Java-index,Core,Core APIs...
# 2

Use a stack trace. Keep in mind this is expensive performance-wise. Here's some code:StackTraceElement [] st=new Exception().getStackTrace();

if(st==null)

//Do something--some runtime environments don't support stack traces

System.out.println("The caller is "+st[1]);

That should do it.

Updownquarka at 2007-7-14 23:23:49 > top of Java-index,Core,Core APIs...