StackTraceElement.getMethodName() Method



Description

The java.lang.StackTraceElement.getMethodName() method returns the name of the method containing the execution point represented by this stack trace element.

Declaration

Following is the declaration for java.lang.StackTraceElement.getMethodName() method

public String getMethodName()

Parameters

NA

Return Value

This method returns the name of the method containing the execution point represented by this stack trace element.

Exception

NA

Example

The following example shows the usage of java.lang.StackTraceElement.getMethodName() method.

package com.tutorialspoint;

import java.lang.*;

public class StackTraceElementDemo {

   public static void main(String[] args) {
      function1();
   }
 
   public static void function1() {
      new StackTraceElementDemo().function2();
   }
 
   public void function2() {
      int i;
      System.out.println("method name : ");

      // print stack trace
      for( i = 1; i <= 3; i++ ) {
         System.out.println(Thread.currentThread().getStackTrace()[i].
         getMethodName());
      }
   }
}   

Let us compile and run the above program, this will produce the following result −

method name :
function2
function1
main
java_lang_stacktraceelement.htm
Advertisements