Let's get right down to business: write 5 methods that call each other. Each method should return the name of the method that called it. Use the stack trace to obtain this information. This name seems so beyond our reach. But we'll figure it out. These are just methods that sequentially call each other, nothing more.
Stack trace revisited
- 6
Locked
Comments (22)
- Popular
- New
- Old
You must be signed in to leave a comment
Andrew Evans
16 February 2022, 03:07
Remember that StackTraceElement is an array that prints out the stack trace (from top to bottom). You can individually print elements of an array...
0
Prop
27 November 2021, 20:12
Each method calls the getStackTrace() method, putting a new element at the top of the stack. Next element of stack would be the current method itself. Then the next method, and so on...
0
miguel
20 September 2021, 18:18
I don't understand why is index "2"...
Someone can explain that??
+2
ImDevin
6 May 2021, 03:36
apparently, index 1 is current method and index 2 is invoking method
+1
Sinisa
12 March 2021, 20:50
After searching, I found out that StackTraceElement has getClassName(), getFileName(), getLineNumber() and getMethodName(). So when executing those methods are going one after another. According to this, getMethodName() should be fourth [3], not third [2] (apparently on different platforms it is stacked different).
So we'll consult a crystal ball a bit and it should be fine 😆
0
Tommy
28 February 2021, 22:00
Geez this was the most confusing task to date for me 😅
+2
Ahmad Abbasi
18 February 2021, 06:04
Why do we have to get the third index and not a different one?
0
Karas Java Developer
11 October 2020, 21:22
Yes, definitelly a useful links from profesor noodles would come in handy in this section.
As hints, basically: Thread.currentThread().getStackTrace() is an array, as explained earlier is in itself a thing.
Thus [] can be use after the (). To clarify June Sung Park get the third element of the array on ALL of them.
Then call the suggested method of the instructions on all of them, same index. It will work.
+2
Alaskian7134
22 December 2020, 10:46
makes no sense to me. why the third on all of them?
0
Karas Java Developer
23 December 2020, 01:39
Because that is the element in the array that corresponds to what the exercise is asking for. Otherwise it will spit out other part of the array and won't work.
Also just remember the third is 2.
0
Sinisa
12 March 2021, 20:38
What is so special in the third element of that array?
0
Karas Java Developer
13 March 2021, 00:45
Per the requirement, each method must return the name of the method that called it.
The third element will get that.
0
Pavel Naumovich
1 October 2020, 21:04
Somebody might find this link useful https://www.tutorialspoint.com/java/lang/stacktraceelement_getmethodname.htm
+3
Michael
29 July 2021, 06:16
Thanks, this link helped me very much to understand the concept of getStackTrace() and it gives the missing information we need to do this task properly without getting into a search marathon on google.
getStackTrace() is, in the words of the Quick Definition popup of intellij, "an array of StackTraceElement, each represents one stack frame." So
is the element at the top of the stack or, in other words, the most recent method invocation in the sequence. is the next method invocation in the sequence giving the name of the calling method. +1
Norbert
20 July 2020, 13:14
Read more about Stack Memory and Heap Space in Java. Google it.
Also through for loop u can see what values StackTrace is keeping.
Hope it is helpful.
0