Java

What are Static Blocks in Java

While programming in Java, there can be a requirement to write a set of codes that the programmer needs to execute during the class loading in memory. For instance, prompting the user with some message before the applied functionalities in the main or prioritizing a vital functionality by separating it. In such cases, static blocks in Java are assistive in streamlining and managing the functionalities at the developer’s end.

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

classMain{
static {
  System.out.println("Hello World");
 }}

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:

publicclass Example {
static {
System.out.println("This is a static block!");
}
publicstaticvoidmain(String[] args) {
System.out.println("This is 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:

classcustom{
staticint i;
static{
i = 10;
System.out.println("Static Block called! ");
}}
publicclass Example {
publicstaticvoidmain(String[] args) {
System.out.println(custom.i);
}}

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:

classcustom{
static{
System.out.println("This is a Static Block");
}
custom(){
System.out.println("This is a Constructor");
}}
publicclass Example {
publicstaticvoidmain(String[] args) {
custom obj1 = new custom();
custom obj2 = new custom();
}}

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.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.