Use of Static Block in Java

In this class, We discuss the Use of Static Block in Java.

The reader should have prior knowledge of static variables in Java. Click here.

Static Block:

A block of code written with the keyword static is called a static block.

Example:

The below diagram shows the example code.

In the above code, we have two static blocks.

We can write any number of static blocks.

Static blocks are executed when the class is loaded to Random access memory.

One of the uses of the static block is to initialize the static variables.

In real life, we have many uses for static blocks.

We can call static methods in static blocks.