Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CallerExample {
- public static void main(String[] args) {
- method1();
- }
- public static void method1() {
- method2();
- }
- public static void method2() {
- StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
- String callerMethodName = stackTrace[2].getMethodName(); // Index 2 to skip getStackTrace and method2
- System.out.println("Caller method: " + callerMethodName);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment