can anybody tell me the code is it true? why i get the int deep =12?
I print out the StackTrace use for-each :
java.lang.Thread.getStackTrace(Thread.java:1559)
com.codegym.task.task09.task0905.Solution.getStackTraceDepth(Solution.java:13)
com.codegym.task.task09.task0905.Solution.main(Solution.java:10)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
If i print out StackTrace by .getMethodName() :
getStackTrace
getStackTraceDepth
main
invoke0
invoke
invoke
invoke
callMainMethod
execute
execute
main
main
package com.codegym.task.task09.task0905;
/*
In the blue depths of the stack trace…
*/
public class Solution {
public static void main(String[] args) throws Exception {
int deep = getStackTraceDepth();
}
public static int getStackTraceDepth() {
StackTraceElement[] ste = Thread.currentThread().getStackTrace();
for(StackTraceElement elements : ste){
System.out.println(elements.getMethodName());
}
return ste.length;
}
}