Sitemap

Static Blocks and Static Initializers in Java: Understanding their Role and Functionality

4 min readJul 2, 2023

--

class MyClass {

static int x = 10; // Static variable

static { // Static block
System.out.println("Static block executed");
x = 20; // Re-initialize x
}

static void myStaticMethod() { // Static method
System.out.println("Static method executed");
}

}
|                        | Static Blocks                                | Static Initializers                            |
|------------------------|-------------------------------------------- |--------------------------------------------- |
| Execution | Executed when the class is loaded | Executed when the class is loaded |
| Initialization Purpose | Initialize static variables or perform setup | Initialize static variables or perform setup |
| Syntax | Code blocks preceded by `static` keyword | Declared at the same level as variables |
| Execution Order | Order they appear in the class | Order of variable declarations |
| Functionality | Can contain complex logic | Limited to initializing variables |
| Example | `static { /* code block */ }` | `public static int myVariable = initialize();` |

In Java, static blocks and static initializers are used to initialize static variables or perform other one-time setup operations for a class. They are executed when the class is first loaded into memory.

Static Blocks:

A static block is a block of code enclosed within curly braces and preceded by the static keyword. It is executed only once when the class is loaded, before any static methods or variables are accessed. The syntax for a static block is as follows:

static {
// code to be executed
}

Static blocks are often used to initialize static variables or perform complex initialization logic that cannot be achieved through a simple variable assignment. For example:

public class MyClass {
public static int myStaticVariable;

static {
// complex initialization logic
myStaticVariable = 42;
}
}

Static Initializers:

Static initializers, also known as static variable initializers, are similar to static blocks but are used to initialize static variables directly. They appear as assignments within the class body, outside any method or block, and are executed in the order they are declared when the class is loaded. Here’s an example:

public class MyClass {
public static int myStaticVariable = initializeStaticVariable();

private static int initializeStaticVariable() {
// complex initialization logic
return 42;
}
}

In this example, the initializeStaticVariable() method is called to initialize the myStaticVariable with a value of 42.

Static initializers are useful when you want to initialize a static variable based on some computation or method call that requires more than a simple assignment.

It’s important to note that static blocks and static initializers are executed only once, when the class is loaded, and they are executed in the order they appear in the code.

Similarities and Difference between Static Blocks and Static Initalizers in Java

In Java, static blocks and static initializers are structures used to initialize static variables of a class. There are similarities and differences between the two:

Get Tajdin Gürdal’s stories in your inbox

Join Medium for free to get updates from this writer.

Similarities:

· Executed when the class is loaded: Both static blocks and static initializers are executed when the class is loaded into memory. They are performed only once and when the class is loaded.

· Used for initializing static variables: Both static blocks and static initializers are used to initialize static variables of a class. They can be used to assign initial values to variables or perform complex initialization logic.

Differences:

· Syntax: Static blocks begin with the keyword static and are written as code blocks enclosed in curly braces. For example:

static {
// code block
}

· On the other hand, static initializers are declared at the same level as variable declarations, and assignments or initialization logic is performed directly on those variables. For example:

public class MyClass {
public static int myStaticVariable = initializeStaticVariable();

private static int initializeStaticVariable() {
// initialization logic
return 42;
}
}

· Execution Order: Static blocks are executed in the order they appear within the class. Each static block defined sequentially is executed when the class is loaded. The order of static blocks is significant. Conversely, static initializers are executed in the order of variable declarations, and they are also performed sequentially when the class is loaded. A second static initializer is executed after the completion of the first initializer.

· Functionality: Static blocks can be used not only to initialize static variables but also to perform complex initialization logic that cannot be done within a non-static block. Any kind of code can be placed within static blocks. On the other hand, static initializers are limited to initializing static variables, and the initial value can be obtained through calculations or method calls.

In summary, static blocks and static initializers are similar tools used to initialize static variables of a class.

Sources:

Static Blocks in Java: https://www.tutorialspoint.com/a-static-initialization-block-in-java

Static Initializer Block in Java: https://www.geeksforgeeks.org/static-blocks-in-java/

Static vs. Instance Initializer Block in Java: https://www.baeldung.com/java-static-instance-initializer-blocks

--

--