Static Initialization Block in Java

Introduction

 
In Java, we have a special block known as a Static Initialization Block. A Static Initialization Block is executed before the main( ) method. This is the main advantage of the Static Initialization Block in Java.
 

Static Initialization Block in Java

 
The following describes the Static Initialization Block in Java:
  • A Static Initialization Block in Java is a block that runs before the main( ) method in Java.
     
  • Java does not care if this block is written after the main( ) method or before the main( ) method, it will be executed before the main method( ) regardless.
     
  • In the entire program, the Static Initialization Block will execute only one time.
     
  • There can be many Static Initialization Blocks in a specific class.
     
  • If we have many Static Initialization Blocks in Java then they are called in a manner in the order they are written in the program.
     
  • This block will not return anything.
     
  • Checked exceptions cannot be thrown.
     
  • We cannot use the "this" keyword since it does not have any instance.
Syntax
 
static
{
.......
.......
.......
}
 
Example of Static Block
  1. package demo;  
  2. public class Demo  
  3. {  
  4.     static double percentage;  
  5.     static int rank;  
  6.     static  
  7.     {  
  8.         percentage = 44.6;  
  9.         rank = 12;  
  10.         System.out.println("STATIC BLOCK");  
  11.     }  
  12.     public static void main(String args[])  
  13.     {  
  14.         Demo st = new Demo();  
  15.         System.out.println("MAIN METHOD");  
  16.         System.out.println("RANK: " + rank);  
  17.     }  
  18. }  
Output
 
Static Block in Java 
 
Example of Multiple Static Blocks
 
The following is an example of multiple static blocks:
  1. package demo;  
  2. public class Demo  
  3. {  
  4.    static  
  5.    {  
  6.        System.out.println("FIRST STATIC BLOCK");  
  7.    }  
  8.    static  
  9.    {  
  10.        System.out.println("SECOND STATIC BLOCK");  
  11.    }  
  12.    static  
  13.    {  
  14.        System.out.println("THIRD STATIC BLOCK");  
  15.    }  
  16.    public static void main(String[] args)  
  17.    {  
  18.    }  
  19. }  
Output
 
multiple static Block in Java
 

Memory Management of Static Block

 
The following is an example of memory management of a static block:
  1. package demo;  
  2. public class Demo  
  3. {  
  4.     static int i;  
  5.     static  
  6.     {  
  7.         i=20;  
  8.     }  
  9.     static void print()  
  10.     {  
  11.         System.out.println(i);  
  12.     }  
  13.     public static void main(String[] args)  
  14.     {  
  15.         print();  
  16.         i=200;  
  17.         print();  
  18.     }  
  19. }  
Output
 
example of memory management 
 
memory management 
 
A Static Initialization Block is not stored on the heap, above in the figure it is clearly visible. It is not stored on the heap because it is executed only once. That is also the reason for the output for the preceding program.
 

Summary

 
This article explains the Static Initialization Block in Java.


Similar Articles