Sitemap

Static Initialization Block in Java

2 min readMay 20, 2025

--

In Java, a static initialization block is a block of code enclosed in braces {} that is preceded by the static keyword. This block of code is executed when the class is loaded into the Java Virtual Machine (JVM). It is a way to perform some initialization tasks for the class or execute some code that needs to run only once when the class is loaded.

  • SIB(Static Initialization block) runs before the main method.
  • And it does not require any invoking statement. (No object creation is required).
  • Whenever there are SIBs in sequence the first one is going to run first
  • We can’t initialize non-static variables inside SIB.
  • We can create an object inside SIB.

Here’s the basic syntax for a static initialization block:

public class MyClass {

// other class members

static {

// code to be executed during class initialization

}

// other class members

}

Key points about static initialization blocks:

  • Execution Timing: The static initialization block is executed when the class is loaded before any static methods are called or any static fields are accessed.
  • No Explicit Invocation: You don’t need to explicitly invoke a static initialization block. It runs automatically when the class is loaded.
  • Multiple Blocks: You can have multiple static initialization blocks in a class, and they are executed in the order they appear in the class.

Here’s a simple example to illustrate the usage:

public class Example {

private static int count;

// Static initialization block

static {

System.out.println(“Static block 1 executed.”);

count = 10;

}

// Another static initialization block

static {

System.out.println(“Static block 2 executed.”);

// additional initialization code

}

// other class members

public static void main(String[] args) {

// main method

}

}

In this example, when the Example class is loaded, the static blocks are executed in the order they appear, printing the specified messages to the console. The count variable is also initialized in the first static block.

If in class we have SIB, IIB, constructor, and method then the sequence of execution will be -

  1. SIB
  2. IIB
  3. Constructor
  4. Method

Example:

Class Example{

//IIB

{

System.out.println(“IIB-1”);

}

//SIB

static

{

Get Manisha Shukla’s stories in your inbox

Join Medium for free to get updates from this writer.

System.out.println(“SIB-1”);

}

//SIB

static

{

System.out.println(“SIB-2”);

}

//constructor

Example(){

System.out.println(“Constructor”);

}

public static void main(String[] args){

Example example = new Example();

System.out.println(“main method”);

}

}

Output:

SIB-1

SIB-2

IIB-1

Constructor

main method

Another Example of creating an object inside SIB:

Class Example{

//IIB

{

System.out.println(“IIB-1”);

}

//SIB

static

{

System.out.println(“SIB-1”);

new Example();

}

//constructor

Example(){

System.out.println(“Constructor”);

}

public static void main(String[] args){

new Example();

System.out.println(“main method”);

}

}

Output:

SIB-1

IIB-1

Constructor

IIB-1

Constructor

main method

Please find the link for IIB -

https://quickqatopics.blogspot.com/2023/11/instance-initialization-block-in-java.html

--

--

Manisha Shukla
Manisha Shukla

Written by Manisha Shukla

I write on software testing( Manual testing ,Automation,API, Java etc.)