我有一个类的静态方法,该方法是从对象或线程的另一个非静态方法中调用的。
有没有办法知道从哪个线程或对象调用了它?
我认为这是不可能的,我一无所获,只是想确认一下。

我的意思是这样的

class CallerID
{
    public static void main(String ...s)
    {
        CallerID ob=new CallerID();
        ob.caller();
    }
    void caller()
    {
        showCaller();
        System.out.println("In this method, ob = "+this);
    }
    static void showCaller()
    {
        //code to get caller object ob like it is printed in method caller()
    }
}

最佳答案

您无法获得对象调用。您可以获取调用对象,线程和方法,但是如果要引用,则必须传递this作为参数。

10-06 10:19
查看更多