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 keyword | Instance 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 | Syntax:
{ |
Static blocks are executed, at the time of class loading | Instance blocks are executed, every time object is created using the new keyword |
Executed only once, at the time class loading | Executed every time the instance is created |
Static 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 |
Since static blocks belong to a class, this and super keywords are not allowed | this 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 loading | Order 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.