Reflection: get invocation object in static method

后端 未结 4 389
轮回少年
轮回少年 2021-01-23 09:38

Is it possible to get an object that invoked static method in this method?

I have this code:

class A{
    static void foo(){
    }
}
A a = new A();
a.foo         


        
4条回答
  •  感动是毒
    2021-01-23 10:23

    No is impossible...the static method don't have the reference, you have to pass it reimplementing the method as:

    class A{
        static void foo(A theObject){
        }
    }
    A a = new A();
    A.foo(a);
    

    and is better don't call the static method from the instance of the object

提交回复
热议问题