Stack trace revisited

  • 6
  • Locked
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.
You can't complete this task, because you're not signed in.
Comments (22)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Andrew Evans
Level 17 , San Jose, Canada
16 February, 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...
Prop
Level 11 , Englewood, United States
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...
miguel
Level 12 , Corredor
20 September 2021, 18:18
I don't understand why is index "2"... Someone can explain that??
ImDevin
Level 15 , Old Town, United States
6 May 2021, 03:36
apparently, index 1 is current method and index 2 is invoking method
Sinisa
Level 11 , Banja Luka, Bosnia and Herzegovina
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 😆
Tommy
Level 10 , Phoenix, AZ, United States
28 February 2021, 22:00
Geez this was the most confusing task to date for me 😅
Ahmad Abbasi
Level 9 , Anaheim, United States
18 February 2021, 06:04
Why do we have to get the third index and not a different one?
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.
Alaskian7134
Level 22 , Iasi, Romania
22 December 2020, 10:46
makes no sense to me. why the third on all of them?
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.
Sinisa
Level 11 , Banja Luka, Bosnia and Herzegovina
12 March 2021, 20:38
What is so special in the third element of that array?
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.
Pavel Naumovich
Level 16 , Chippenham, United Kingdom
1 October 2020, 21:04
Somebody might find this link useful https://www.tutorialspoint.com/java/lang/stacktraceelement_getmethodname.htm
Michael
Level 10 , Dresden, Germany
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
getStackTrace()[1]
is the element at the top of the stack or, in other words, the most recent method invocation in the sequence.
getStackTrace()[2]
is the next method invocation in the sequence giving the name of the calling method.
Norbert
Level 9 , Krakow, Poland
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.