0

In C++, you can use __FUNCTION_NAME__ to get the name of the function that contains __FUNCTION_NAME__.

Is there an equivalent in Java? It could, in Java, be possible to do something with this and reflection. Is there something simpler though?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483

2 Answers2

0

Thread.currentThread().getStackTrace() will usually contain the method you’re calling it from but there are pitfalls

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StackTraceElement.html

java seeker
  • 1,246
  • 10
  • 13
0
Thread.currentThread().getStackTrace()[ste.length - 1 - depth].getMethodName();

depth = 0 (zero) will give current method

also

System.out.println((new Throwable()).getStackTrace()[0].toString());

Sample output:

com.junk.Junk3.main(Junk3.java:12)
i100
  • 4,529
  • 1
  • 22
  • 20