Monday, June 1, 2020

Instance initializer block

Instance block: Instance block is used to write the logics and initialize the instance variables, both are done during object creation time.
Syntax:
{
//logics here
}

why do we need instance block?

  • Instance block is executed during object creation before constructor execution. i.e. first instance block then constructor
  • Constructor logics are specific to object (i.e. three object, three different constructors) , but instance block logics are common for all objects (instance block logic is common for all object)
  • Inside the class, it is possible to declare multiple instance block
  • Instance block execution depends on object creation not on constructors execution. Instance block is executed for every object creation.

Advantages of instance block
  • Advantage # 1: Write the logics and those logics are executed during object creation:
Example I: During object creation time if any instance blocks are there, first instance block will be executed, once the instance block is executed then only constructor is executed
class Test1{
       Test1(){
             System.out.println("0-argument constructor");
       }
       {
             System.out.println("instance block");
       }
}
public class InstanceBlockDemo1 {
public static void main(String[] args) {
       //Test1 t = new Test1();  ;// named object creation
       new Test1();             // nameless object creation
  
}
}
Output:
instance block
0-argument constructor
Why do we need instance block?

Example 2: Instance block is executed for every object creation
class Test2{
       Test2(){
             System.out.println("0-argument constructor");
       }
       Test2(int a){
             System.out.println("1-arguement constructor");
       }
       Test2(int a, int b){
             System.out.println("2-argument constructor");
       }
       {
             System.out.println("instance block");
       }
}
public class InstanceBlockDemo2 {
public static void main(String[] args) {
       Test2 t1 = new Test2();                   // instance block then constructor
       Test2 t2 = new Test2(10);                // instance block then constructor
       Test2 t3 = new Test2(10, 20);           // instance block then constructor
}
}
Output:
instance block
0-argument constructor
instance block
1-arguement constructor
instance block
2-argument constructor
Example IV: Instance block execution depends on object creation not on constructor execution
class Test3{
Test3(){
       this(10);
       System.out.println("0-argument constructor");
}
Test3(int a){
       System.out.println("1-argument constructor");
}
Test3(int a, int b){
       System.out.println("2-argument constructor");
}
{
       System.out.println("instance block");
}
}
public class InstanceBlockDemo3 {
public static void main(String[] args) {
       new Test3();
}
}
Output:
instance block
1-argument constructor
0-argument constructor

Example V: Inside the class, we can write multiple instance block and execution order is from top to bottom
class Test4{
       Test4(){
             System.out.println("0-argument constructor");
       }
       Test4(int a){
             System.out.println("1-argument constructor");
       }
       {
             System.out.println("instance block-1");
       }
       {
             System.out.println("instance block-2");
       }
}
public class InstanceBlockDemo4 {
public static void main(String[] args) {
       new Test4();
       new Test4(10);
}
}
Output:
instance block-1
instance block-2
0-argument constructor
instance block-1
instance block-2
1-argument constructor

Advantage # 2: Initialize the variables:
class Test5{
       int eid;
void display(){
       System.out.println(eid);
}
//instance block initializing value to variable during object creation
{
eid = 111;
}
}
public class InstanceBlockDemo5 {
public static void main(String[] args) {
       new Test5().display();
}
}
Output:
111

No comments:

Post a Comment