Static Block in Java
When a block is decorated or associated with the word static, it is called a static block. Static Block is known as the static clause. A static block can be used for the static initialization of a class. The code that is written inside the static block run once, when the class is getting loaded into the memory.
Invoking a Static Block
Now the question arises, how to invoke the static block? To invoke the static block, there is no specific way since the static block gets executed automatically, whenever in the memory the class is loaded. Observe the following illustration.
Inside the static block. Inside the constructor of the class. Inside the print method. Inside the constructor of the class.
Explanation: By looking at the output, it is evident that the print statement of the static block gets executed first. Then, the print statement of the constructor of the class, after that print statement of the print() method, and after that, again, the constructor's print statement. Also, note that inside the main() method, we have invoked the constructor and the print() method of the class explicitly. However, no statement is written for invoking the static block. It shows that the static block is executed automatically, and that too before invoking the constructor of the class.
Example - 1
Let's see an example to get a better understanding of static block.
Inside the static block. - 1 Inside the static block. - 2 Inside the static block. - 3 Inside the constructor of the class. Inside the method foo. Inside the constructor of the class.
Explanation: It is evident by looking at the output that static blocks get executed first, and that too in the order the static blocks are written in the above code. It is also evident that more than one static block can exist in the code.
Example - 2
Let's see another example of a static block.
/StaticBlock2.java:21: error: non-static method foo() cannot be referenced from a static context foo(); ^ /StaticBlock2.java:22: error: non-static variable st cannot be referenced from a static context System.out.println(st); ^ 2 errors
Explanation: Static block can only access the static variables or static methods and will give an error if one tries to access any non-static variable/method.
In order to run the program without any error, add the keyword static to the variable st, and method for as shown in the following program.
Inside the method foo. 9 Inside the static block. - 1 Inside the constructor of the class.
Static Block and Main Method
We know that static blocks are loaded automatically (see the above examples) when the class is loaded. In other words, there is no need for a method to invoke the static blocks. Then the question arises is there any need of the main() method? The answer depends on the GDK version the user is using.
If the user is using GDK version 1.06 or previous ones, the static blocks get executed without mentioning the main() method in the code. However, the program gives an error if the user is using the GDK version, which is higher than 1.06.
Observe the following two programs.
The print statement gets executed without main method.
Error: Main method not found in class StaticBlock5, please define the main method as: public static void main(String args) or a JavaFX application class must extend javafx.application.Application