For every thread JVM will create a separate stack, all method calls performed by thread will be stored in that stack. Each entry in the stack is called one Stack Frame. To represent Stack Frame in Java we use a class called ‘StackTraceElement‘. After completing every method call JVM removes the corresponding entries from the stack. After completing all method calls JVM destroys the empty stack and terminates the program normally. Below is an example program to demonstrate call stack.
RuntimeStackExample :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.vidvaan.corejava.exception04.callstack; public class RuntimeStackExample { public static void main(String[] a) { System.out.println("main Method Enter"); doStuff(); System.out.println("main Method Exit"); } public static void doStuff() { System.out.println("doStuff Method Enter"); doMoreStuff(); System.out.println("doStuff Method Exit"); } public static void doMoreStuff() { System.out.println("doMoreStuff Method Enter"); System.out.println("domoreStuff.....SomeStatements"); System.out.println("doMoreStuff Method Exit"); } } |
