"Hi! Today I'll tell you what a stack trace is. But first I need to tell you what a stack is."
"Imagine a pile of papers – directives for a certain employee. You can put a new task on top of the pile, or you can take a task from the top. This means that tasks won't be executed in the order they were received. The task placed on the pile most recently will be the first to be executed. Structuring the elements of a collection this way forms a stack."
"Java has a special collection for that – Stack. It's a collection that has methods to 'add an element' and 'take (get) an element'. As you may have guessed, the element that was added last will be the first to be taken."
"Sounds rather straightforward."
"Great. Now I'll explain what a stack trace is."
"Imagine that in a Java program method A called method B, which called method C, which in turn called method D. To exit method B, we must first exit method C, and to do that – we must first exit method D. This behavior resembles a stack."
"Why do you say it resembles it?"
"To get to some task in the middle of our stack of papers, for example, you first need to execute all the tasks lying on top of it."
"There's some similarity, but I'm not sure I understand everything correctly."
"Look. A stack is a set of elements. Like pieces of paper in a pile. To take the third piece of paper from the top, you first need to take the second, and for that, you need to take the first. You can always put and take pieces of paper, but you always have to take the top paper first."
"The same is true for function calls. Method A calls method B, which calls method C. To exit A, you must first exit B, and to do that, you need to exit C."
"Wait. If I understand what you're saying, the entire concept of a stack boils down to 'take the piece of paper that was added last' and 'you can only exit the method you most recently entered'. Is that accurate?"
"Yes. The sequence of function calls is known as the ‘call stack' or simply the ‘stack'. The last function called is the first function to end. Let's dig into an example."
Get and display the current call stack: |
---|
|
Result: |
|
"OK. I get everything about function calls. But what is this StackTraceElement?"
"The Java Machine keeps track of all function calls. For that, it has a special collection – the stack. When one function calls another, the Java Machine puts a new StackTraceElement object onto the stack. When a function finishes, that element is removed from the stack. This means that the stack always stores up-to-date information about the current state of the 'stack of function calls'."
"Each StackTraceElement object contains information about the called method. In particular, you can get the method name using the getMethodName method."
"You can see how this works in the example above:
1) We get the call stack.
2) We use a for-each loop to go through it. I hope you haven't forgotten what that is.
3) We output the method names to System.out."
"Fascinating! And not too complicated either. Thank you, Rishi!"
GO TO FULL VERSION