Get name of current method being executed in Java
Getting the name of the current method being executed can be effective for dealing with exceptions and debugging purposes.
In Java, it is possible to get the name of the current method being executed using various approaches explained in the section below.
1. Using the Thread Stack Trace
A stack trace can be considered a list of stack frames that depicts a moment during the code execution. It contains some data about the method that had been called. The getStackTrace() function returns an array of the elements. The second element from the returned array elements consists of the name of the method of the current thread.
FileName: GetMethodName.java
// Java program to Get the name of the current method
// Being executed
// using the getStackTrace()
public class GetMethodName {
// creates a static method work
public static void work()
{
// currentThread() method returns the name
// Of the current method
// Being executed
// At the first position in Stack Trace
String methodName = Thread.currentThread()
.getStackTrace()[1]
.getMethodName();
// Print the Method Name
System.out.println("The name of current method being executed: "
+ methodName);
}
// Main method
public static void main(String[] args)
{
// function call
work();
}
}
Output:
The name of the current method being executed: work
2. Using the Throwable Stack Trace
- Using Throwable Class
It is the parent or superclass for all the errors and packages in Java programming. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. It also contains a view of the stack under execution of its thread when it was created. The 0th position of the array, which is the top of the stack, gives the name of the current method being executed.
FileName: GetMethodName1.java
// Java program to Get the name of the current method
// Being executed
// using Throwable Class
public class GetMethodName1 {
// creates a static method work
public static void work()
{
// getStackTrace() method returns the name
// Of the current method
// Being executed
// At the 0th position in Stack Trace
String methodName = new Throwable()
.getStackTrace()[0]
.getMethodName();
// Print the Method Name
System.out.println("The name of current method being executed: "
+ methodName);
}
// Main method
public static void main(String[] args)
{
// function call
work();
}
}
Output:
The name of the current method being executed: work
- Using the Exception class
The Exception extends the Throwable class. Hence, it can also be utilized to get the current method's name.
FileName: GetMethodName2.java
// Java program to Get the name of the current method
// Being executed
// using the Exception class
public class GetMethodName2 {
// creates a static method work
public static void work()
{
// getStackTrace() method returns the name
// Of the current method
// Being executed
// At the 0th position in Stack Trace
String methodName = new Exception()
.getStackTrace()[0]
.getMethodName();
// Print the Method Name
System.out.println("The name of current method being executed: "
+ methodName);
}
// Main method
public static void main(String[] args)
{
// function call
work();
}
}
Output:
The name of the current method being executed: work
3. Using the getEnclosingMethod() method
a) By the Object class
The java.lang.Class.getEnclosingMethod() returns a function object that depicts the immediate enclosing function of the underlying class if that class is local or anonymous. Else, it returns null. Note that it comes with some overhead as it involves the creation of a new anonymous class in the background.
FileName: GetMethodName3.java
// Java program to Get the name of the current method
// Being executed
// using the Object class
public class GetMethodName3 {
// creates a static method work
public static void work()
{
// It returns a Method object that depicts the
// the instantly enclosing method of the method class
String methodName = new Object() {}
.getClass()
.getEnclosingMethod()
.getName();
// Print the Method Name
System.out.println("The name of current method being executed: "
+ methodName);
}
// Main method
public static void main(String[] args)
{
// Function call
work();
}
}
Output:
The name of the current method being executed: work
- By the Inner class
An inner class can also be declared inside a method to get the reference of that class.
FileName: GetMethodName4.java
// Java program to Get the name of the current method
// Being executed
// using the Internal class
public class GetMethodName4 {
// creates a static method work
public static void work()
{
// Internal class
class Internal {
}
// It returns a Method object that depicts the
// the instantly enclosing method of the method class
String methodName = Internal.class
.getEnclosingMethod()
.getName();
// Print the Method Name
System.out.println("The name of current method being executed: "
+ methodName);
}
// Main method
public static void main(String[] args)
{
// Function call
work();
}
}
Output:
The name of the current method being executed: work