How to get the name of the currently executing method in Java

1 Answer

0 votes
public class MyClass {
    public static void test() {
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    
    public static void main(String args[]) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        System.out.println(methodName);
        
        test();
    }
}
        
        
        
        
/*
run:
        
main
test
        
*/

 



answered Oct 7, 2023 by avibootz

Related questions

1 answer 284 views
1 answer 190 views
2 answers 240 views
1 answer 166 views
1 answer 256 views
...