This blog will illustrate the usage of static blocks in Java.
What are “Static Blocks” in Java?
A block is allocated as “static” by associating the static keyword with it. These blocks have higher precedence than “main” such that they are executed before the “main()” method.
Syntax
In the above syntax, the included block is assigned as “static” and will be invoked before the main.
Important Considerations Regarding “Static Blocks”
- Static blocks in Java are invoked automatedly when the class is loaded in memory.
- These are executed only once, even if multiple class objects are created.
- There is no limitation/restriction on the number of static initialization blocks within a class.
- These blocks can be utilized to initialize the static variables as well.
Example 1: Utilization of “Static Blocks” in Java
In this example, the static block can be used to observe the sequence in which it can be executed with respect to main:
In the above code block, simply include a “static” block accumulating the stated message and in the next step, display the given message in “main”.
Output
In this output, it can be analyzed that the static block is invoked before the main.
Example 2: Executing Static Value Within “Static Block” in Java
In this particular illustration, a static value can be initialized in the static block and later on displayed in the main:
In the above lines of code:
- First of all, create a class named “custom”.
- Within the class, specify a “static” variable and initialize it in the allocated “static” block.
- Lastly, invoke the initialized variable in the main by referring to the class in which it is contained.
Output
In the above outcome, it can be seen that, likewise, the “static” block is executed first, and the initialized value in this block is also displayed, respectively.
Example 3: Executing “Static Block” Before Constructor
In this example, the static block can be specified before the constructor, and its precedence and execution can be observed accordingly:
In the above lines of code:
- Likewise, define a class named “custom”.
- Now, specify the “static” block and include the class constructor, i.e., “custom()” having the stated messages, respectively.
- In the main, create two objects of the created class to invoke the class functionalities in the order of precedence accordingly.
Output
In this output, the following considerations can be carried out:
- The static block is given higher precedence as compared to the contained constructor.
- Two objects of the class are created, but the static block is executed once, as discussed before.
Conclusion
The “static blocks” in Java execute only once when a class is loaded into memory and are executed before the “main()” method. The discussed examples in the article prove that these blocks have higher precedence than the main and the class constructor, respectively, and are invoked only once, irrespective of the number of created objects. This blog discussed the usage of static blocks in Java.