All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Get Current Method Name

Last Updated : Mar 11, 2024

Java Get Current Method Name

In this article we will show you the solution of java get current method name, a StackTraceElement object represents the stack trace of the current thread as retrieved by Java's Thread.currentThread().getStackTrace() method.

A StackTraceElement object is returned as an array as part of this method.

The top most StackTraceElement in the array has a getMethodName() method that can be used to determine the name of the current method.

We will now discuss the idea of how to get the current method name in java with an example.

Step By Step Guide On Java Get Current Method Name :-

Code 1

public class TC {
    public static void foo()
    {
        class Inner {
        };
        String nameofcurrmethod = Inner.class
                                      .getEnclosingMethod()
                                      .getName();
        System.out.println("Name of current method: "
              + nameofcurrmethod);
    }
    public static void main(String[] args)
    {
        foo();
    }
}

Code 2

package com.talkerscode.example;
public class talkerscode{
 public static void main(String[] args) {
  System.out.println("In method : main()");
  Thread thread = Thread.currentThread();
  StackTraceElement[] stackTraceElements = thread.getStackTrace();
  System.out.println("Method name is : "+
    stackTraceElements[1].getMethodName());
  methodA();
 }
 private static void methodA() {
  System.out.println("In method : methodA()");
  Thread thread = Thread.currentThread();
  StackTraceElement[] stackTraceElements = thread.getStackTrace();
  System.out.println("Method name is : "+
    stackTraceElements[1].getMethodName());
  methodB();
 }
 private static void methodB() {
  System.out.println("In method : methodB()");
  Thread thread = Thread.currentThread();
  StackTraceElement[] stackTraceElements = thread.getStackTrace();
  System.out.println("Method name is : "+
    stackTraceElements[1].getMethodName());
  methodC();
 }
 private static void methodC() {
  System.out.println("In method : methodC()");
  Thread thread = Thread.currentThread();
  StackTraceElement[] stackTraceElements = thread.getStackTrace();
  System.out.println("Method name is : "+
    stackTraceElements[1].getMethodName());
  completeStackTrace();
 }
 private static void completeStackTrace() {
  System.out.println("In method : completeStackTrace()");
  Thread thread = Thread.currentThread();
  StackTraceElement[] stackTraceElements = thread.getStackTrace();
  System.out.println();
  System.out.println("Printing Complete Stack Trace ... \n");
  for(int i = 0; i< stackTraceElements.length; i++) {
   System.out.println("Method at "+ i +" : "+
     stackTraceElements[i].getMethodName());
  }
 }
}
  1. The class TC is declared.
  2. The method foo is defined, which has a locally defined class Inner.
  3. Using Inner.class.getEnclosingMethod().getName(), the name of the current method is retrieved and stored in the variable nameofcurrmethod.
  4. System.out.println() prints the name of the current method to the console in foo ().
  5. A method called foo() is defined as the main method.
  6. When foo is called, its locally defined class Inner is not instantiated, but instead its enclosing method (i.e. foo) is accessed using getEnclosingMethod(), and its name is retrieved using getName().
  7. The retrieved name is then printed out to the console.

Conclusion :-

As a result, we have successfully learned how to get current method name in java with example.

The purpose of this tutorial is to provide you with some examples of how to find out what method is currently being executed.

A demonstration of how stack traces and getEnclosingMethod() can be used is provided in this article.

I hope this article on java get current method name helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪