Actually this code is working perfectly
package com.codegym.task.task09.task0902;
/*
Stack trace revisited
*/
public class Solution {
public static void main(String[] args) throws Exception {
method1();
}
public static String method1() {
method2();
StackTraceElement[] methods = Thread.currentThread().getStackTrace();
//System.out.println("From method1: " + methods[methods.length-1].getMethodName());
return methods[methods.length-1].getMethodName();
}
public static String method2() {
method3();
StackTraceElement[] methods = Thread.currentThread().getStackTrace();
//System.out.println("From method2: " + methods[methods.length-2].getMethodName());
return methods[methods.length-2].getMethodName();
}
public static String method3() {
method4();
StackTraceElement[] methods = Thread.currentThread().getStackTrace();
//System.out.println("From method3: " + methods[methods.length-3].getMethodName());
return methods[methods.length-3].getMethodName();
}
public static String method4() {
method5();
StackTraceElement[] methods = Thread.currentThread().getStackTrace();
//System.out.println("From method4: " + methods[methods.length-4].getMethodName());
return methods[methods.length-4].getMethodName();
}
public static String method5() {
StackTraceElement[] methods = Thread.currentThread().getStackTrace();
//System.out.println("From method5: " + methods[methods.length-5].getMethodName());
return methods[methods.length-5].getMethodName();
}
}