Run-time Stack mechanism in Java

Eeverytime a process or a code or a thread needs to run in Java, a runtime stack is created so as to store the operations performed while executing the thread.

Every entry in the run-time stack is known as stack frame or activation record. Once a function has been called by the process, its associated data is deleted from the runtime stack.

Once all the functions have been called, the runtime stack will be empty. This means it needs to be removed from the memory.

At this point in time, the runtime stack is destroyed and then the thread is also terminated.

A termination of thread can happen once the thread completes (volunatily) or forcefully (abnormal termination).

The order of destruction of elements in the runtime stack is the opposite order of the creation of entries in the runtime task.

When thread operates normally, and completes execution, the main function is called and its entry will be stored in the runtime stack. Similarly, other functions (if any) are called and their entries are stored. When the functions have completed execution, it is time to remove the entries from the runtime stack. The last function that was executed is the first one whose entry is removed.

When thread terminates abnormally, it means all the lines of the code couldn’t be successfully executed. This means an exception is raised since an error is encountered. Below is an example of the same −


 Live Demo

public class Demo{
   public static void main(String[] args){
   public static void test(){
      System.out.println("This is a test method.");
   public static void test_2(){
      System.out.println("This is a method that divides 10 by 0.");


Exception in thread "main" java.lang.ArithmeticException: / by zero
at Demo.test_2(
at Demo.test(
at Demo.main(

A class named Demo contains the main function wherein the ‘test’ function is called. The ‘test’ function is defined wherein ‘test_2’ function is called. A function named ‘test_2’ is defined wherein a number is tried to be divided by 0. This result in an exception that is printed on the console. Hence, the control doesn’t reach the ‘println’ line to print the error message.

Updated on: 07-Jul-2020


Kickstart Your Career

Get certified by completing the course

Get Started