☰ See All Chapters |
Java Blocks
There are two types of blocks, they are
Instance blocks/Instance Initialization blocks
Static blocks /Static Initialization blocks
Instance blocks
Instance Initialization blocks runs when an instance is created, we can use it to initialize instance member to initialize some value. An instance initialization block runs once every time a new instance/object is created.
class SmallInit { int y; // instance initialization block { y = 8; } } |
Use of Instance blocks
When you have multiple constructors or overloaded constructors in your class, And you have certain common operation or initialization in each constructor then you can write those common operations in instance initialization block.
This way we make sure that irrespective of any constructor invoked that common operations would be executed or taken care by JVM itself.
Key points-rules about Instance blocks
Initialization or static blocks don't have names, they can't take arguments, and they don't return anything.
You can have many initialization blocks in a class.
Instance initialization blocks run every time a class instance is created.
Instance initialization blocks executes before the constructor of that class invoked and after the call to all super ().
Instance initialization block code runs right after the call to super () in a constructor, in other words, after all super constructors have run.
It is important to note that unlike methods or constructors, the order in which initialization blocks appear in a class matters. Constructor and super will we studied later.
Static blocks
Static blocks is set/block of statements to perform operations immediately after class loaded. Static blocks executes only once immediately after JVM loads that particular class.
class SmallInit { static int x; // static initialization block static { x = 7; } } |
Use of Static blocks
It can be useful for any kind of operation which required to be run only once for your application.(e.g. DB Driver initialization)
It can be used for some environment or security check before the actual application gets loaded.
Key points-rules about Static blocks
Static Initialization or static blocks don't have names, they can't take arguments, and they don't return anything.
You can have many static initialization blocks in a class.
It is important to note that unlike methods or constructors, the order in which static initialization blocks appear in a class matters.
class Init { Init(int x) { System.out.println("1'st constructor with arguements"); }
Init() { System.out.println("2'nd constructor with no arguements"); }
static { System.out.println("1'st static Block"); }
{ System.out.println("1'st instance Block"); }
{ System.out.println("2'nd instance Block"); } static { System.out.println("2nd static Block"); }
public static void main(String[] args) { new Init(); new Init(7); } } |
Output:
1'st static Block 2nd static Block 1'st instance Block 2'nd instance Block 2'nd constructor with no arguements 1'st instance Block 2'nd instance Block 1'st constructor with arguements |
All Chapters