Get name of current method being executed in Java

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 indexString nameofCurrMethod =newThrowable().getStackTrace()[0].getMethodName();System.out.println("Name of current method: "+ nameofCurrMethod);}publicstaticvoidmain(String[] args){// call functionfoo();}}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 ClasspublicclassGFG {publicstaticvoidfoo(){// getStackTrace() method return current// method name at 0th indexString nameofCurrMethod =newException().getStackTrace()[0].getMethodName();System.out.println("Name of current method: "+ nameofCurrMethod);}publicstaticvoidmain(String[] args){// call functionfoo();}}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 foopublicstaticvoidfoo(){// this method return a Method object representing// the instantly enclosing method of the method classString nameofCurrMethod =newObject() {}.getClass().getEnclosingMethod().getName();System.out.println("Name of current method: "+ nameofCurrMethod);}publicstaticvoidmain(String[] args){// call functionfoo();}}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 ClasspublicclassGFG {// create a static method foopublicstaticvoidfoo(){// Local inner classclassInner {};// this method return a Method object representing// the instantly enclosing method of the method classString nameofCurrMethod = Inner.class.getEnclosingMethod().getName();System.out.println("Name of current method: "+ nameofCurrMethod);}publicstaticvoidmain(String[] args){// call functionfoo();}}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 foopublicstaticvoidfoo(){// Thread.currentThread() return current method name// at 1st index in Stack TraceString nameofCurrMethod = Thread.currentThread().getStackTrace()[1].getMethodName();System.out.println("Name of current method: "+ nameofCurrMethod);}publicstaticvoidmain(String[] args){// call functionfoo();}}Output:
Name of current method: foo



