2

Can I recognize my calling object?
I can create stacktrace for classname of A but how can I access an object of A?

class A{
    void method(){
        new B().method();
    }
}

class B{
    void method(){
        // can i recognize Object A ????
    }
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
Frank M.
  • 997
  • 1
  • 10
  • 19
  • 4
    Don't do that! It's not a good idea to produce that kind of tight coupling. Why do you think you need this? – Joachim Sauer Nov 04 '13 at 13:07
  • i know, i know.. but sometimes - do you know a way to find Object of A? – Frank M. Nov 04 '13 at 13:29
  • Sometimes what? The only 2 parts that I could reasonably think of requiring that information are security checks (see `SecurityManager`, that's already done) and possibly logging (see your favorite logging framework, also already done). – Joachim Sauer Nov 04 '13 at 13:31
  • Can you not pass B as a method parameter into B::method(A a). Not that this is usually wise – Richard Tingle Nov 04 '13 at 14:02

3 Answers3

4

Use a StrackTraceElement:

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();

The top element is what you are looking for.

Also go through this.

Community
  • 1
  • 1
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • Why do you link to some third-party site that just replicates what's already [in the official documentation](http://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.html)? Especially since your answer already links to the JavaDoc itself. – Joachim Sauer Nov 04 '13 at 13:12
  • Just because of that small summary of methods in a tabular form. Provided the reference as a source so it is not plagiarism – Hanky Panky Nov 04 '13 at 13:14
  • That's *exactly* the same information that's provided here: http://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.html#method_summary. So there's no need to refer to a third-party site at all (plus: posting textual information as an image is bad for searchability, accessibility and copy-and-pasting). – Joachim Sauer Nov 04 '13 at 13:15
  • i look for calling object and not only for calling class. – Frank M. Nov 04 '13 at 13:25
  • http://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection – Hanky Panky Nov 04 '13 at 13:26
  • @FrankM.: that's not possible. – Joachim Sauer Nov 04 '13 at 13:32
2
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()  

javadoc:

The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.

and the documentation of what you can get from each StackTraceElement

http://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.html

getClassName()
getFileName()
getLineNumber() 
getMethodName()
RamonBoza
  • 8,898
  • 6
  • 36
  • 48
1

If you really need to do this, then you should incorporate it into the code.

class A{
    void method(){
        new B().method(this);
    }
}

class B{
    void method(A a){
        System.out.println("I recognize 'a' as " + A);
    }
}
MadConan
  • 3,749
  • 1
  • 16
  • 27