Instance Initialization blocks Vs Static Initialization blocks In Java Example Tutorial

Static Initialization Blocks vs Instance Initialization blocks In Java:

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 concepts in detail in the following articles

  • Initialization blocks (Static and Instance blocks)
  • The execution order of constructor and Initialization blocks

Let us detail out the difference between Static Initialization blocks vs Instance Initialization blocks in tabular form below.

Static Initialization blocks
Instance Initialization blocks
This is also referred to as “static blocks” or “static initializer”This is also referred to as “instance blocks” or “instance initializer”
Static blocks are a bundle of valid Java statements within {curly braces} prefixed with the static keywordInstance blocks are a bundle of valid Java statements within {curly braces}
Note: Just opening and closing curly braces and there is no keyword
Syntax:

static
{
// bundle of Java logics
}

Syntax:

{
// bundle of Java logics
}

Static blocks are executed, at the time of class loadingInstance blocks are executed, every time object is created using the new keyword
Executed only once, at the time class loadingExecuted every time the instance is created
Static blocks can be used to initialize static data members and invoke static methods onlyInstance blocks can be used to initialize instance data members and invoke instance methods
Since static blocks belong to a class, this and super keywords are not allowedthis keyword is used to access instance data members in instance blocks
Order 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 an object created

If you like SoftwareTestingo and would like to contribute something to this community, then you can also write an article using our Contact us page or mail your article to admin@softwaretestingo.com. So that we can review your article and that also appears on the SoftwareTestingo.com main page and help other Testers.

Please Improve this article, if you find anything incorrect by commenting on the comment box and we are happy to work on the article to maintain the accuracy and improvement.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment