Java – Static Initialization blocks v/s Instance Initialization blocks

In this article, we will list the difference between Static Initialization blocks and Instance Initialization blocks in Java

Before moving ahead with the differences, read the detailed concepts about these cpncepts in detail in the following articles

Let us detail out difference between Static Initialization blocks v/s Instance Initialization blocks in tabular form below,

1. Static Initialization block v/s Instance Initialization block

Sr. No.Static Initialization blocksInstance Initialization blocks
1This is also referred as “static blocks” or “static initializerThis is also referred as “instance blocks” or “instance initializer
2Static blocks are bundle of valid Java statements within {curly braces} prefixed with static keywordInstance blocks are bundle of valid Java statements within {curly braces}

 

Note: Just opening and closing curly braces and there is no keyword

3Syntax:

 

static {

// bundle of Java logics

}

Syntax:

 

{

// bundle of Java logics

}

4Static blocks are executed, at the time of class loadingInstance blocks are executed, every time object is created using new keyword
5Executed only once, at the time class loadingExecuted every time instance is created
6Static blocks can be used to initialize static data members and invoke static methods only Instance blocks can be used to initialize instance data members and invoke instance methods
7Since static blocks are belongs to class, this and super keywords are not allowedthis keyword are used to access instance data members in instance blocks
8Order of execution: Static blocks are always executed first comparing with instance blocks, at the time class loadingOrder of execution: Instance blocks are executed after static blocks, as instance blocks executed only when object created

Read more about Initialization blocks in Java

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Abstract classes and methods with example
Java - Constructors, Initialization blocks and their execution order