Home » Tip Bank » Java
  • Comment on this Article
Mar 11, 2019
  • facebook
  • twitter
  • in
  • Email



Building the Right Environment to Support AI, Machine Learning and Deep Learning


Know the Details of the Current Method Being Executed

You might be interested to know where the current code flow is. The getMethodName() method in the getStackTrace(), if used as illustrated below, is really helpful.

As always, specific use cases to be defined and the available features to be used accordingly.

Code snippet:

public class CurrentMethod
public static void main(String args[])
CurrentMethod currentMethod = new CurrentMethod();

private void proceed()
System.out.println( "Current method(): " + getCurrentMethodName(0)); 
System.out.println( "Current method(): " + getCurrentMethodName(1)); 
System.out.println( "Current method(): " + getCurrentMethodName(2)); 
System.out.println( "Current method(): " + getCurrentMethodName(3)); 
//The index needs to be provided carefully, else it results in ArrayIndexOutOfBoundsException

String getCurrentMethodName(int index) {
return Thread.currentThread().getStackTrace()[index].getMethodName(); 

Expected output:

[root@mypc]# java CurrentMethod
Current method(): getStackTrace
Current method(): getCurrentMethodName
Current method(): proceed
Current method(): main
Sridhar M S
Submit a Tip
Browse "Java" Tips
Browse All Tips