Which class invoked my static method?

Suppose that I have a Java class with a static method, like so:

class A
{
    static void foo()
    {
        // Which class invoked me?
    }
}

And suppose further that class A has an arbitrary number of subclasses:

class B extends A { }
class C extends A { }
class D extends A { }
...

Now consider the following method invocations:

A.foo();
B.foo();
C.foo();
D.foo();
...

My question is, how can method foo() tell which class is invoking it?


Asked by: Michael544 | Posted: 28-01-2022






Answer 1

It can't, and that's part of the problem with static methods. As far as the compiler is concerned A.foo() and B.foo() are exactly the same thing. In fact, they compile down to the same bytecode. You can't get more similar than that.

If you really need this sort of information, use a singleton and turn foo() into an instance method. If you still like the static syntax, you can build a facade A.foo().

Answered by: Clark112 | Posted: 01-03-2022



Answer 2

Although you can't find out which class the static method was invoked on, it is possible to find out which class actually invoked the method at runtime:

static void foo()
{
   Throwable t = new Throwable();
   StackTraceElement[] trace = t.getStackTrace();
   String className = trace[1].getClassName();
   Class whoCalledMe = null;
   try
   {
      whoCalledMe = Class.forName( className );
   }
   catch( Exception e )
   {
   }
}

I'm not saying that this is good practice and its probably not great from a performance perspective either, but it should work. Don't know if that helps you any ...

Answered by: Owen748 | Posted: 01-03-2022



Answer 3

class A
{
    static void foo(A whoIsCalingMe)
    {
        // Which class invoked me?
    }
}

Answered by: Kelvin508 | Posted: 01-03-2022



Similar questions

aop - How to know, the number of times a particular method is invoked in java

Is there any way to know how many times a instance of a class has invoked its member method. I think(not sure), one way is to have a dedicated a member variable for a method, But that will not be feasible if we have so many methods. For example: class A{ public void someMethod(){ } } and i have a instance say A a = new A();


Can a main() method of class be invoked from another class in java

Can a main() method of class be invoked in another class in java? e.g. class class1{ public static void main(String []args){ } } class class2{ public static void main(String []args){ class1.main(); } }


java - Hadoop job fails when invoked by cron

I have created the following shell script for invoking a hadoop job: #!/bin/bash /opt/hadoop/bin/hadoop jar /path/to/job.jar com.do.something <param-1> ... <param-n> & wait %1 STATUS=$? if [ $STATUS -eq 0 ] then echo "SUCCESS" | mailx -s "Status: "$STATUS -r "mail@mysite.com" "mail@mysite.com" exit $STATUS else echo "FAILED" | mailx -s "Status: "$STATUS -r "mail@mysite.com" "...


java - RMI - who invoked the method

is there a way to know what server/client invoked a method on the server? Problem: I have a completely connected graph of server, and when a command comes in from a client on one of the server nodes, I forward it to the rest of the server nodes on the graph. I want to only forward the commands if and only if it's coming from a client and not another server.


How to know the parent class of the invoked java program?

I am working on a distributed application and getting an exception in the main method of a class. How do I know which java program has invoked it ? I tried debugging the distributed application, but could not figure it out.


java - Spring Aspect fails when join point is invoked in new thread

I'm using Spring 3.0.5 with an Around aspect. The @Around aspect works perfectly. The AOP expression targets the interfaces of a bunch of beans. The aspect executes some logic before and after the invokation: @Around(...) public Object monitor(ProceedingJoinPoint pjp) throws Throwable { // some code Obj o = pjp.proceed(); // some code } ...


java - How is GUI code being invoked before my Scanner?

I'm having some trouble with getting input from the command line before opening a GUI window. I asked this question previously on Apple Exchange but was sent here after we determined it to be a Programming problem. Basically I'm running a Scanner to get user input before I open up a window but it starts the program, switching spaces on my Mac, and then I have to switch back to the workspace with the terminal in it to answe...


java - How can I know if a method was invoked on the unit under test?

I'm writing a new class (Derived), in TDD, using mockito and I have the following case: Class Base: public abstract class Base<T>{ //....... protected final T baseCreate(T entity){ // implementation } } Class Derived ( This is the class that I'm writing using TDD ): public class Derived extends Base<MyObject> { //.......


using a 2d array in a java api when the api will be invoked from a javascript client

I am working to create a simple API in Java using Apache CXF- in one function of the API, one of the inputs is required to be a 2-d array. This API will be invoked from a javascript client. I am confused about the following-- (a) I dont want to restrict the size of the 2-d array parameter in the Java function. What should be the type of this parameter- should I use an arraylist? Or something else? C...


java - Jersey get method which will be invoked

I'm trying to implement ContainerRequestFilter which would check if method which should be invoked is annotated with @Authorize and if it is, invoke method which will check if user is authorized and based on return value either return not Authorized or proceed with request. Now I wonder is there an easy way to get info on which method will be invoked if I proceed with the request?






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top