27

I was wondering if there is a way to know the method name being executed at run time?

For instance, inside a private void doSomething (String s) method, I'd like to know that I am executing the doSomething (String s) method.

Paul T.
  • 714
  • 1
  • 9
  • 20
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • 1
    This really isn't something your code should require. I'd very much advise rethinking your design so that you don't need it. – Michael Borgwardt Jan 09 '11 at 20:28
  • 1
    If you are inside that method you already know the name, right? :) If you want to know the caller method's name, Jigar's answer below is the one you are looking for. Just use 1 instead of 0 as index. – Umut Utkan Jan 09 '11 at 20:28

3 Answers3

66

Since JDK1.5, you don't need an Exception to get the StackTrace,
you can get it with Thread.currentThread().getStackTrace()]:

   public class Test2 {
     public static void main(String args[]) {
        new Test2().doit();
     }
     public void doit() {
        System.out.println(
           Thread.currentThread().getStackTrace()[1].getMethodName()); // output : doit
     }
   }
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
RealHowTo
  • 34,977
  • 11
  • 70
  • 85
  • 1
    This is a better solution, as you don't have to create the exception to get the stack. – rfeak Jan 09 '11 at 20:43
  • 2
    Not sure why this isn't working for me...it prints out "getStackTrace" for me - using Java 1.5 – Zack Macomber Feb 05 '13 at 20:21
  • @Zack Macomber, What is the output : `StackTraceElement [] ste = Thread.currentThread().getStackTrace(); for (StackTraceElement s : ste) { System.out.println(s); }` , are you using a Sun JRE ? – RealHowTo Feb 05 '13 at 22:26
  • 1
    @RealHowTo - I was able to use '[2]' instead of '[1]' to get the method name - I use IBM's RSA so I think I may be using IBM's version of Java – Zack Macomber Feb 07 '13 at 15:59
  • That's what I got - a) [Ljava.lang.StackTraceElement;@429c6ae0 b) -getThreadStackTrace how come? – golosovsky Apr 24 '15 at 17:05
  • Is it always certain that I will get the name of the method at `index 1` of `stacktrace`? – Ram Patra May 12 '15 at 12:03
  • @Ram swaroop, it depends on the JVM vendor (Oracle, IBM,...) – RealHowTo May 12 '15 at 21:34
  • @RealHowTo Okay, so is there any universal solution to this? At least in Java 8? – Ram Patra May 13 '15 at 04:55
  • 1
    I am trying to print the method name in android activity methods . I had to use `Thread.currentThread().getStackTrace()[2].getMethodName()` – Devrath Apr 07 '17 at 07:20
  • What is the performance implications for this? I mean, will it be better to create a string with the method name instead of using this solution? Especially when the **output depends on the JVM vendor**. – Shubham A. May 10 '17 at 06:10
10
System.out.println(new Exception().getStackTrace()[0].getMethodName());

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • useGetStacktrace()[1] to get the calling methods name. A bit more useful :) – Daniel Jan 09 '11 at 20:24
  • @Daniel, is that what was asked for? Note that 1) there is no _guarantee_ that the stacktrace is accurate, and 2) that this has previously been a very expensive operation. It should be better in recent JVM's. – Thorbjørn Ravn Andersen Jan 09 '11 at 20:28
  • 1
    Really? When would it possible that you get an inaccurate stack trace occurs? Or is it just not guaranteed you get one? I never had these problems. – Daniel Jan 09 '11 at 20:35
  • It think it's important to precise that this solution is only good for JDK **>= 1.4** and **< 1.5**. For JDK **>= 1.5** you don't have to instanciate a new `Exception` each time, which saves resources (checkout *RealHowTo*'s answer above). – Mickäel A. Jan 10 '15 at 15:58
-2
Method lastMethodCalled = this.getClass().getEnclosingMethod();
  • 1
    This doesn't do what he was asking for. See [this link](http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getEnclosingMethod()) – job Oct 10 '12 at 20:22