In this expression
return Thread.currentThread().getStackTrace()[2].getMethodName();
I don't understand what the [2] means in each of the functions. I have read the documentation, I have searched but I am not able to find anything that explains that. Thank you very much in advance.
Xavi Martin
Nivel 22
Dude, Could some kind soul help me?
En discusión
Comentarios (3)
- Populares
- Nuevas
- Antiguas
Debes iniciar sesión para dejar un comentario
Thomas
22 noviembre 2022, 17:17
Lisa explained that excellent (in german) here: https://codegym.cc/de/help/16556
maybe use an online translator like deepl.com
or my try in english: https://codegym.cc/help/15181
+1
Xavi MartinExpert
30 noviembre 2022, 08:07
Sorry but no resolve my question. I don't understund the [2] assignament in the code declaration.
Thanks.
0
Thomas
30 noviembre 2022, 08:40
[2] specifies the 3rd element in the array. getStackTrace() returns an array that contains objects representing the stacktrace at the moment the getStackTrace() method gathers that information.
The first element in the array [0] represents the upmost method on the stack. And that's getStackTrace()
The second element in the array [1] is the next method on the stack, means the method that called getStackTrace. And [2] represents the method that called the method that invoked getStackTrace().
Again, look at the examples Lisa posted, run the code, look at the output. Examine...read the API docs, maybe that helps (it is somewhat very technical and you first need to learn to read it without giving up).
+1