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 Class
public
class
GFG {
Â
ÂÂ Â Â Â
public
static
void
foo()
   Â
{
       Â
// getStackTrace() method return
       Â
// current method name at 0th index
       Â
String nameofCurrMethod =
new
Throwable()
                                     Â
.getStackTrace()[
0
]
                                     Â
.getMethodName();
Â
ÂÂ Â Â Â Â Â Â Â
System.out.println(
"Name of current method: "
           Â
+ nameofCurrMethod);
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(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
Â
Âpublic
class
GFG {
Â
ÂÂ Â Â Â
public
static
void
foo()
   Â
{
       Â
// getStackTrace() method return current
       Â
// method name at 0th index
       Â
String nameofCurrMethod =
new
Exception()
                                     Â
.getStackTrace()[
0
]
                                     Â
.getMethodName();
Â
ÂÂ Â Â Â Â Â Â Â
System.out.println(
"Name of current method: "
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â
+ nameofCurrMethod);
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(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 Class
public
class
GFG {
Â
ÂÂ Â Â Â
// create a static method foo
   Â
public
static
void
foo()
   Â
{
       Â
// this method return a Method object representing
       Â
// the instantly enclosing method of the method class
       Â
String nameofCurrMethod =
new
Object() {}
                                     Â
.getClass()
                                     Â
.getEnclosingMethod()
                                     Â
.getName();
Â
ÂÂ Â Â Â Â Â Â Â
System.out.println(
"Name of current method: "
ÂÂ Â Â Â Â Â Â Â Â Â Â
+ nameofCurrMethod);
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(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Â
Â
Âpublic
class
GFG {
Â
ÂÂ Â Â Â
// create a static method foo
   Â
public
static
void
foo()
   Â
{
       Â
// Local inner class
       Â
class
Inner {
       Â
};
Â
ÂÂ Â Â Â Â Â Â Â
// 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);
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(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()Â
public
class
GFG {
Â
ÂÂ Â Â Â
// create a static method foo
   Â
public
static
void
foo()
   Â
{
       Â
// 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);
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(String[] args)
   Â
{
       Â
// call function
       Â
foo();
   Â
}
}
Output:
Name of current method: foo