How to know which function called a function in java

I have a function A() and B() both of which makes call to a function C() from inside them.

Now when i am inside C() how can i know if the call came from A() or B().
What are the possible ways i can achieve it.

Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Radek BaranowskiFull-stack Java DeveloperCommented:
run your program, do a thread dump with kill -3 <pid>, open the dump in thread dump analyzer and look at stack.


implement some clever dummy logging for dev puprposes and print out call order to the log or console.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rohit BajajAuthor Commented:
I want to know about the function from inside C(). That is if A() called me then do ....if B() called me then do ....
There's something wrong with your design if you need to know/do that. The reason you can be sure of that is that

a. there's no mechanism that can tell you that without the use of some horrible kludge like parsing the call stack
b. absence of said mechanism == absence of need for said mechanism
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Radek BaranowskiFull-stack Java DeveloperCommented:
Just trace down place in A() when you call C()

then just one line above put

System.our.println("I am calling C right now.");

Open in new window

do the same in B()

you can also put in C()

System.our.println("I am in C right now.");

Open in new window

then run the whole thing and check out log/console to see what is message order.

You can also add to C() declaration a parameter String whoCalledMe;

then invoke C  from A with

C(<other args>,"A called me");

Open in new window

and from B

C(<other args>,"B called me");

Open in new window

and from C do


Open in new window

and you'll see it in the log/console
I think we may be making the solution of this too hard.  It may just be that you can add a parameter to C (a bit like Radek suggested).  So your code becomes:

C(boolean doExtraSteps) {
    if (doExtraSteps) {
        // We know we were called by A and now do the extra steps
    } else {
       // We know we were called by B and do whatever B requires

And modify the callers to pass in this extra flag:

A(true) ;
B(false) ;

In general, the idea is you pass something into C() which indicates how it was called.  That's the general principle.

awking00Information Technology SpecialistCommented:
Can you post the call from A to C?
awking00Information Technology SpecialistCommented:
I didn't read dpearson's response very closely, but I believe he has the right idea. Add a boolean value of true to the call from A or false to the call from B and include a boolean parameter in C. My initial idea was to just modify the call from A and overload the C method to do A stuff with one set of parameters and B stuff with another, but that would require unnecessary effort.
Can anyone show me a real world example in the context of professional software of having to pass parameters to determine which method has called which?
awking00Information Technology SpecialistCommented:
CEHJ asks a very pertinent question. To have a method that needs to determine what other method called it seems to be of poor design at best.

Can you post the calling statements to C from A and B describing any parameter types passed? Also, give a literal explanation of why  C needs to know whether it was called by A or B.
Rohit BajajAuthor Commented:
This question came to my mind when i was working on android. I was on the lines that onResume() method is called from onPause() method or onStart() method. So i wanted to know which of  onPause() or onStart() called onResume(). But this thinking was wrong. onResume() is indeed called after onStart() but it does not call it.

So the original way i came to this question was wrong. Now having any practical use case for such a thing.
Ah that's different then. For that, you can do

new Throwable().printStackTrace();

Open in new window

Can anyone show me a real world example in the context of professional software of having to pass parameters to determine which method has called which?
I'm not sure if this question was aimed at my response, but I was simply suggesting the normal role that all parameters play - we parameterize methods just so we can produce different behavior when they're called from different parent methods.  And so of course the inverse is true - if you wish to know - you can work back to how you were called (and hence who called you) by inspecting the parameters.

in any case, for an Android app the onResume() etc. methods are all part of the framework, so you can't change the signatures and pass any parameters.  This really was more of a "stack trace" type question and CEHJ has posted the way we dump stack traces in Java (yep - it's ugly!).

I'm not sure if this question was aimed at my response

No, it was really because i was concerned that the question was being asked as a result of design problems.
That's what can happen when the goal is unstated:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.