Static Block and Non-Static Block

Static Initialization Block in Java:

When a block is declared with the static keyword, it is called static block in Java. It is a normal block of code that is enclosed in braces ({ }) and is preceded by a keyword “static”.

Static block is also known as static initialization block or static initializer block in Java.

The static block gets executed only once by JVM when the class is loaded into the memory by Java ClassLoader. The syntax to declare static block in Java program is as follows:

Syntax:

static {
   // statement of block.
 }

Why Static block is executed before Main method?


When we execute a particular class, JVM performs two actions at the runtime. They are as:

1. JVM loads the corresponding dot class file (byte code) into memory.

2. During the dot class file loading into memory, static block is executed. After loading the dot class file, JVM calls the main method to start execution. Therefore, static block is executed before the main method.

In other words, we can also say that static block always gets executed first in Java because it is stored in the memory at the time of class loading and before the object creation.

Use of Static block in Java:

There are mainly three uses of static block in java that are as follows:

1. The purpose of using a static initialization block is to write that logic inside static block that is executed during the class loading.

2. It is mostly used for changing default value of static variables.[TBD]

3. It is used to initialize static variables of the class.

Order of execution of Multiple Static blocks in Java:

A class can have any number of static initialization blocks that will execute in the same sequence as written in the program.

That is, the order of execution of multiple static initialization blocks is executed automatically from top to bottom during the dot class file loading.

Non-Static Block:

When the block is declared without using any modifier, then it is treated as the non-static block is first executed before the constructor is executed. Non-static block directly access the static variable [TBD]and instance variable.

Points to remember Non-Static Blocks:

  • The Non-static blocks are class level blocks which do not have any prototype.
  • The need for a non-static block is to execute any logic whenever an object is created irrespective of the constructor.[TBD]
  • The Non-static blocks are automatically called by the JVM for every object creation in the java stack area.[TBD]
  • We can create any number of Non-static blocks in Java.
  • The order of execution of non-static blocks is an order as they are defined.

Difference between Static block and Instance block:

Static blockInstance block
Static block is also known as a static initialization blockinstance block is also known as instance initialization block or non-static block.
They execute before the instance blockinstance block executes after the static blocks.
Only static variables can be accessed inside the static blockboth static and non-static variables can be accessed inside the instance block.
Static blocks execute when the class is loaded into the memory instance blocks execute only when instance of the class is created.
 ‘this’ keyword cannot be used in the static block[TBD]this keyword can be used in the instance block.[TBD]

Reference:

https://www.scientecheasy.com/2020/06/static-block-java.html/

https://ecomputernotes.com/java/data-type-variable-and-array/non-static-block

https://www.geeksforgeeks.org/static-blocks-in-java/

https://www.tutorialspoint.com/what-is-the-order-of-execution-of-non-static-blocks-with-respect-to-a-constructor-in-java#:~:text=The%20Non%2Dstatic%20blocks%20are,in%20the%20java%20stack%20area.

Leave a comment

Design a site like this with WordPress.com
Get started