Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (3.4k points)

Is there a way to get the name of the currently executing method in Java?

1 Answer

0 votes
by (46k points)

You can  check out  Thread.currentThread().getStackTrace() it will normally include the method you’re calling 

For instance:

public Thread​(ThreadGroup group,

              Runnable target,

              String name,

              long stackSize,

              boolean inheritThreadLocals)

And here are a few lines from Java docs

Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this thread is permitted to return a zero-length array from this method.

Browse Categories