How to find the caller of a method in Java.



class
{
    public void display() 
    {
        StackTraceElement ste[] = Thread.currentThread().getStackTrace();
        System.out.println("method is called from " + ste[2]);
    }
}

public class GetCurrentClassName 
{
    public static void main(String args[]) 
    {
        A a = new A();
        a.display();
    }
}



No comments:

Post a Comment