Core JAVA

Instance initializer block in Java

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">Apart from methods and constructors&comma; Initialization Blocks are the third place in a Java Program where operations can be performed&period; Initialization Blocks come in two flavors&colon;<&sol;p>&NewLine;<ol>&NewLine;<li><b>Static Initialization Blocks&colon;<&sol;b> Runs first when the class is first loaded&period; Declared by using the keyword &OpenCurlyDoubleQuote;<i><b>Static<&sol;b><&sol;i>”<&sol;li>&NewLine;<li><b>Instance Initialization Blocks&colon;<&sol;b> Runs every time when the instance of the class is created&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><b>Code Snippet&colon;<br &sol;>&NewLine;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">class InitDemo &NewLine;&lbrace; &NewLine; static int y&semi; &NewLine; int x&semi; &NewLine; &sol;&sol;Static Initialization Block &NewLine; static &NewLine; &lbrace; &NewLine; y&equals;10&semi; &NewLine; &rcub; &NewLine; &sol;&sol; Instance Initialization Block &NewLine; &lbrace; &NewLine; x&equals;20&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<div style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; white&semi; background-position&colon; initial initial&semi; background-repeat&colon; initial initial&semi; border&colon; 0px&semi; color&colon; black&semi; font-family&colon; Georgia&comma; 'Times New Roman'&comma; Times&comma; serif&semi; font-size&colon; 15px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; 24&period;625px&semi; margin&colon; 0px 0px 1em&semi; orphans&colon; auto&semi; outline&colon; 0px&semi; padding&colon; 0px&semi; text-align&colon; start&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; vertical-align&colon; baseline&semi; white-space&colon; normal&semi; widows&colon; auto&semi; word-spacing&colon; 0px&semi;">Instance initialization block code runs right after the call to&nbsp&semi;<b style&equals;"background-color&colon; transparent&semi; background-position&colon; initial initial&semi; background-repeat&colon; initial initial&semi; border&colon; 0px&semi; font-size&colon; 15px&semi; margin&colon; 0px&semi; outline&colon; 0px&semi; padding&colon; 0px&semi; vertical-align&colon; baseline&semi;">super&lpar;&rpar;&nbsp&semi;<&sol;b>in a constructor&comma; in other words&comma; after all super constructors have run&period; The order in which initialization blocks appear in a class matters&period; If a class has more than one they all run in the order they appear in the class file&period;<&sol;div>&NewLine;<div style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; white&semi; background-position&colon; initial initial&semi; background-repeat&colon; initial initial&semi; border&colon; 0px&semi; color&colon; black&semi; font-family&colon; Georgia&comma; 'Times New Roman'&comma; Times&comma; serif&semi; font-size&colon; 15px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; 24&period;625px&semi; margin&colon; 0px 0px 1em&semi; orphans&colon; auto&semi; outline&colon; 0px&semi; padding&colon; 0px&semi; text-align&colon; start&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; vertical-align&colon; baseline&semi; white-space&colon; normal&semi; widows&colon; auto&semi; word-spacing&colon; 0px&semi;">Some rules to remember&colon;<&sol;div>&NewLine;<ul style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; white&semi; background-position&colon; initial initial&semi; background-repeat&colon; initial initial&semi; border&colon; 0px&semi; color&colon; black&semi; font-family&colon; Georgia&comma; 'Times New Roman'&comma; Times&comma; serif&semi; font-size&colon; 15px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; 24&period;625px&semi; list-style&colon; disc&semi; margin&colon; 0px 0px 1em 25px&semi; orphans&colon; auto&semi; outline&colon; 0px&semi; padding&colon; 0px&semi; text-align&colon; start&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; vertical-align&colon; baseline&semi; white-space&colon; normal&semi; widows&colon; auto&semi; word-spacing&colon; 0px&semi;">&NewLine;<li style&equals;"background-color&colon; transparent&semi; background-position&colon; initial initial&semi; background-repeat&colon; initial initial&semi; border&colon; 0px&semi; font-size&colon; 15px&semi; margin&colon; 0px&semi; outline&colon; 0px&semi; padding&colon; 0px&semi; vertical-align&colon; baseline&semi;">Initialization blocks execute in the order they appear&period;<&sol;li>&NewLine;<li style&equals;"background-color&colon; transparent&semi; background-position&colon; initial initial&semi; background-repeat&colon; initial initial&semi; border&colon; 0px&semi; font-size&colon; 15px&semi; margin&colon; 0px&semi; outline&colon; 0px&semi; padding&colon; 0px&semi; vertical-align&colon; baseline&semi;">Static Initialization blocks run once when the class is first loaded&period;<&sol;li>&NewLine;<li style&equals;"background-color&colon; transparent&semi; background-position&colon; initial initial&semi; background-repeat&colon; initial initial&semi; border&colon; 0px&semi; font-size&colon; 15px&semi; margin&colon; 0px&semi; outline&colon; 0px&semi; padding&colon; 0px&semi; vertical-align&colon; baseline&semi;">Instance Initialization blocks run every time a class instance is created&period;<&sol;li>&NewLine;<li style&equals;"background-color&colon; transparent&semi; background-position&colon; initial initial&semi; background-repeat&colon; initial initial&semi; border&colon; 0px&semi; font-size&colon; 15px&semi; margin&colon; 0px&semi; outline&colon; 0px&semi; padding&colon; 0px&semi; vertical-align&colon; baseline&semi;">Instance Initialization blocks run after the constructor’s call to&nbsp&semi;<b style&equals;"background-color&colon; transparent&semi; background-position&colon; initial initial&semi; background-repeat&colon; initial initial&semi; border&colon; 0px&semi; font-size&colon; 15px&semi; margin&colon; 0px&semi; outline&colon; 0px&semi; padding&colon; 0px&semi; vertical-align&colon; baseline&semi;">super&lpar;&rpar;&period;<&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<div style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; white&semi; background-position&colon; initial initial&semi; background-repeat&colon; initial initial&semi; border&colon; 0px&semi; color&colon; black&semi; font-family&colon; Georgia&comma; 'Times New Roman'&comma; Times&comma; serif&semi; font-size&colon; 15px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; 24&period;625px&semi; margin&colon; 0px 0px 1em&semi; orphans&colon; auto&semi; outline&colon; 0px&semi; padding&colon; 0px&semi; text-align&colon; start&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; vertical-align&colon; baseline&semi; white-space&colon; normal&semi; widows&colon; auto&semi; word-spacing&colon; 0px&semi;">&NewLine;<&sol;div>&NewLine;<p><b>Instance initializer block&colon;<&sol;b><br &sol;>&NewLine;Instance Initializer block is used to initialize the instance data member&period; It run each time when object of the class is created&period;<br &sol;>&NewLine;The initialization of the instance variable can be directly but there can be performed extra operations while initializing the instance variable in the instance initializer block&period;<&sol;p>&NewLine;<p><b>Why use instance initializer block&quest;<&sol;b><br &sol;>&NewLine;Suppose I have to perform some operations while assigning value to instance data member e&period;g&period; a for loop to fill a complex array or error handling etc&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&sol;&sol;Program of instance initializer block that initializes values to the instance variable &NewLine; &NewLine;class Car&lbrace; &NewLine; int speed&semi; &NewLine; &NewLine; Car&lpar;&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;"speed is "&plus;speed&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &lbrace; &NewLine; speed &equals; 120&semi; &NewLine; &rcub; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; Car c1&equals;new Car&lpar;&rpar;&semi; &NewLine; Car c2&equals;new Car&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b>Output&colon;<&sol;b><br &sol;>&NewLine;speed is 120<br &sol;>&NewLine;speed is 120<&sol;div>&NewLine;<p>&NewLine;There are three places in java where you can perform operations&colon;<&sol;p>&NewLine;<ol>&NewLine;<li>method<&sol;li>&NewLine;<li>constructor<&sol;li>&NewLine;<li>block<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>&NewLine; <b>Flow of invoked of instance initializer block &amp&semi; constructor<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">class Car&lbrace; &NewLine; int speed&semi; &NewLine; &NewLine; Car&lpar;&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;"constructor is invoked"&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &lbrace; &NewLine; System&period;out&period;println&lpar;"instance initializer block invoked"&rpar;&semi; &NewLine; speed &equals; 120&semi; &NewLine; &rcub; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; Car c1&equals;new Car&lpar;&rpar;&semi; &NewLine; Car c2&equals;new Car&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b>Output&colon;<&sol;b><br &sol;>&NewLine;instance initializer block invoked<br &sol;>&NewLine;constructor is invoked<br &sol;>&NewLine;instance initializer block invoked<br &sol;>&NewLine;constructor is invoked<&sol;div>&NewLine;<p>&NewLine;In the above example&comma; it seems that instance initializer block is firstly invoked but NO&period; Instance intializer block is invoked at the time of object creation&period; The java compiler copies the instance initializer block in the costructor after the first statement super&lpar;&rpar;&period; So firstly&comma; constructor is invoked&period; Let&&num;8217&semi;s understand it by the figure given below<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;04&sol;initializer&period;png"&sol;><&sol;div>&NewLine;<p><b>Rules for instance initializer block &colon;<&sol;b><br &sol;>&NewLine;There are mainly three rules for the instance <b>initializer <&sol;b>block&period; They are as follows&colon;<&sol;p>&NewLine;<ol>&NewLine;<li>The instance <b>initializer <&sol;b>block is created when instance of the class is created&period;<&sol;li>&NewLine;<li>The instance <b>initializer <&sol;b>block is invoked after the parent class constructor is invoked &lpar;i&period;e&period; after<b> super&lpar;&rpar;<&sol;b> constructor call&rpar;&period;<&sol;li>&NewLine;<li>The instance <b>initializer <&sol;b>block comes in the order in which they appear&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;super-keyword-in-java&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;exception-handling-and-checked-and&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;super-keyword-in-java&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;exception-handling-and-checked-and&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '505'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago