package com.codegym.task.task09.task0902;
/*
Stack trace revisited
*/
import java.lang.StackTraceElement;
public class Solution {
public static void main(String[] args) throws Exception {
method1();
}
public static String method1() {
method2();
return Thread.currentThread().getStackTrace().getMethodName();
}
public static String method2() {
method3();
return Thread.currentThread().getStackTrace().getMethodName();
}
public static String method3() {
method4();
return Thread.currentThread().getStackTrace().getMethodName();
}
public static String method4() {
method5();
return Thread.currentThread().getStackTrace().getMethodName();
}
public static String method5() {
return Thread.currentThread().getStackTrace().getMethodName();
}
}
Why is my code not Compiling
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Hashirama
25 January 2019, 14:17
Try this in one of those methods and see:
StackTraceElement ste = Thread.currentThread().getStackTrace();
for (StackTraceElement elements : ste )
sout(elements.getMethodName());
Then try this also:
sout(Thread.currentThread().getStackTrace());
Try these also:
sout(Thread.currentThread().getStackTrace()[0]);
sout(Thread.currentThread().getStackTrace()[1]);
sout(Thread.currentThread().getStackTrace()[2]);
sout(Thread.currentThread().getStackTrace()[2].getMethodName());
Hope you know what sout means?
+1
Khurram
30 December 2018, 08:36
you cannot call getMethodName function like it is done here, Thread.currentThread().getStackTrace() is used to initialize the StackTraceElement array, after that on each element of the array you can call the getMethodName function, arrayName[index].getMethodName()
0