Getting name of currently executing method is useful for handling exceptions and debugging purposes.
Below are different methods to get currently executing method :
- Using Throwable Stack Trace :
- Using Throwable Class : In Java, Throwable class is the superclass of all exceptions and errors in java.lang package. Java Throwable class provides several methods like addSuppressed(), getMessage(), getStackTrace(), getSuppressed(), toString(), printStackTrace() etc.
We can get an array of stack trace elements representing the stack trace pertaining to throwable by calling getStackTrace() on a Throwable instance. At 0th index element of the array represents the top of the stack, which is the latest method call in the sequence.// Java program to demonstrate// Getting name of current methodÂ// using Throwable ClasspublicclassGFG {   Âpublicstaticvoidfoo()   Â{       Â// getStackTrace() method return       Â// current method name at 0th index       ÂString nameofCurrMethod =newThrowable()                                     Â.getStackTrace()[0]                                     Â.getMethodName();       ÂSystem.out.println("Name of current method: "           Â+ nameofCurrMethod);   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// call function       Âfoo();   Â}}Output:
Name of current method: foo
- Using Exception Class
We can also use Exception class which extends Throwable class.// Java program to demonstrate// Getting name of current methodÂ// using Throwable ClassÂÂpublicclassGFG {   Âpublicstaticvoidfoo()   Â{       Â// getStackTrace() method return current       Â// method name at 0th index       ÂString nameofCurrMethod =newException()                                     Â.getStackTrace()[0]                                     Â.getMethodName();       ÂSystem.out.println("Name of current method: "            Â+ nameofCurrMethod);   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// call function       Âfoo();   Â}}Output:
Name of current method: foo
- Using Throwable Class : In Java, Throwable class is the superclass of all exceptions and errors in java.lang package. Java Throwable class provides several methods like addSuppressed(), getMessage(), getStackTrace(), getSuppressed(), toString(), printStackTrace() etc.
- Using getEnclosingMethod() method
- By Object Class : We can use Class.getEnclosingMethod(), this method returns a Method object representing the instantly enclosing method of the prime class. But this come with a expressive overhead as it involves creating a new anonymous inner class behind the scenes.
// Java program to demonstrate// Getting name of current methodÂ// using Object ClasspublicclassGFG {   Â// create a static method foo   Âpublicstaticvoidfoo()   Â{       Â// this method return a Method object representing       Â// the instantly enclosing method of the method class       ÂString nameofCurrMethod =newObject() {}                                     Â.getClass()                                     Â.getEnclosingMethod()                                     Â.getName();       ÂSystem.out.println("Name of current method: "          Â+ nameofCurrMethod);   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// call function       Âfoo();   Â}}Output:
Name of current method: foo
- By Inner Class : We can also define a inner class within a method to get Class reference.
// Java program to demonstrate// Getting name of current methodÂ// using Inner ClassÂÂÂpublicclassGFG {   Â// create a static method foo   Âpublicstaticvoidfoo()   Â{       Â// Local inner class       ÂclassInner {       Â};       Â// this method return a Method object representing       Â// the instantly enclosing method of the method class       ÂString nameofCurrMethod = Inner.class                                     Â.getEnclosingMethod()                                     Â.getName();       ÂSystem.out.println("Name of current method: "             Â+ nameofCurrMethod);   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// call function       Âfoo();   Â}}Output:
Name of current method: foo //This java program on our machine because inner class // have some restriction for security purpose
- By Object Class : We can use Class.getEnclosingMethod(), this method returns a Method object representing the instantly enclosing method of the prime class. But this come with a expressive overhead as it involves creating a new anonymous inner class behind the scenes.
- Using Thread Stack Trace : The Thread.getStackTrace() method returns array of stack trace elements. The second element of the returned array of stack trace contains name of method of current thread.
// Java program to demonstrate// Getting name of current methodÂ// using Thread.getStackTrace()ÂpublicclassGFG {   Â// create a static method foo   Âpublicstaticvoidfoo()   Â{       Â// Thread.currentThread() return current method name       Â// at 1st index in Stack Trace       ÂString nameofCurrMethod = Thread.currentThread()                                     Â.getStackTrace()[1]                                     Â.getMethodName();       ÂSystem.out.println("Name of current method: "           Â+ nameofCurrMethod);   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// call function       Âfoo();   Â}}Output:
Name of current method: foo
