Who called me?

  • 3
  • Locked
The call stack may not be the most beautiful construct... But why not? It makes sense—you just need to get used to it. Let's get used to tasks! This time, you need to write five methods that call each other. Each method should return the line number of the code from which it was called. To do this, use the element.getLineNumber() method.
You can't complete this task, because you're not signed in.
Comments (11)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Prop
Level 11 , Englewood, United States
27 November 2021, 22:21
Why is it that the Stack Trace Element referred to differs from the line number that is returned with the getLineNumber() method. e.g.: at method5() when you call Thread.currentThread().getStackTrace()[2].getMethodName(), this would return the name of the third element on the stack, which would be method4(). However, when calling Thread.currentThread().getStackTrace()[2].getLineNumber(), this returns line number 29, which is where method5() is called. Shouldn't the getLineNumber() called on element 3 of the stack return line 24 where method4() is actually called?
Anonymous #10775689
Level 14 , United Kingdom
2 August 2021, 08:34
I understand the importance of self directed research and finding answers, but this isn't a great Level so far. At least explain some of the reasons behind the [2] value etc. Otherwise the whole course could just be one link to docs.oracle.com
Michael
Level 10 , Dresden, Germany
3 August 2021, 07:12
I am following the same resolution strategy in the code challenges. Since level 8, codegym is not preparing properly for the tasks, thereby we loose so much valuable time by searching around that I am more and more in doubt about the effectiveness of the whole course.
ImDevin
Level 15 , Old Town, United States
6 May 2021, 03:51
it feels like CG just wants us to get familiar with what it looks like and just keep practicing writing it. Maybe in's and out's of how it is used would be coming later?? At least, it's clear that getStackTrace()[2] means the invoking method. I hope someday, we'll laugh about this. Happy coding! :)
Switch/Cypher
Level 25 , Bexleyheath, United Kingdom
19 September 2020, 09:55
It's all very well to say 'use the element.getline method' but there's been no explanation of HOW you use that method....... No explanation that you need the index. No explanation of what the index should be. So much stuff has been covered so thoroughly in this course, but with this we were just given a single unexplained line of code, that doesn't help to solve the tasks.
Andrei
Level 41
10 November 2020, 11:30
Oh man, so much trial and error for this task, so much documenting and headbanging, haha... I had to figure out what the index did and what it represented.. what to do with the element.. what is the element... lol...
Andrei
Level 41
10 November 2020, 11:33
As you can see, my way of doing it was way more complicated than it should have been lol.
StackTraceElement stack = Thread.currentThread().getStackTrace()[INDEX];
       elNo = stack.getLineNumber(); //previosuly defined class static variable
       System.out.println(elNo); //to see what index is printing for trial and error, haha.
Janusz
Level 11 , Radomsko, Poland
23 December 2019, 16:00
Just don't ask why!
Janusz
Level 11 , Radomsko, Poland
23 December 2019, 15:59
/* Comment has been deleted */
Roman
Level 41
24 December 2019, 09:14
Posting the solution in the comments is prohibited.
Gábor
Level 14 , Budapest, Hungary
4 October 2019, 21:39
Was it mentioned anywhere in the lecture that that to call this method i need a different INDEX...?? Anyway what is that INDEX for ?? :O Thread.currentThread().getStackTrace()[INDEX].getLineNumber();