Instance blocks in Java

Spread the love

Elements of a class in Java-

Java is a class based language where the entire code is written inside a class.

Any class written in Java consists of the following 5 elements-

  1. Variables
  2. Methods
  3. Constructors
  4. Instance blocks
  5. Static blocks

 

In this post, we will discuss about the instance blocks of Java in detail.

 

Instance blocks in Java-

 

Instance block is an element of a class which serves the same two purposes as served by the constructors.

These two purposes are-

  1. Just like constructors, instance blocks can be used to initialize the instance variables during object creation.
  2. Just like constructors, instance blocks can also be used to write the logic of the program which has to be executed automatically for all objects during their creation. (Main Purpose)

 

When do instance blocks execute?

 

If any instance block exists in the program, then for every object during its creation, first of all that instance block will be executed and then its specific constructor will be executed.

 

Need of instance blocks-

 

Because instance blocks are executed whenever the object of any kind is created, so if we want to write a logic which we want to execute on the creation of all kinds of objects, then using instance blocks is a good idea to avoid writing the same logic inside every constructor.

 

Note-

 

Although instance blocks can be used for the initialization of objects but we generally don’t use them for the initialization of objects because they can not accept the parameters.

However, if we still use instance blocks for the purpose of initialization, then all the objects will have to be initialized with the same values which is practically useless.

That is why, we mainly use constructors for the initialization of objects.

 

Syntax-

{
   // This is called as instance block
}

 

Program illustrating the use of instance blocks-

class Test
{
   Test( )                                            // This constructor will get executed for 1st kind of object 
   {
      System.out.println("0 argument constructor");
   }

   Test(int a)                                        // This constructor will get executed for 2nd kind of object
   {
      System.out.println("1 argument constructor");
   }

   Test(int a , int b)                                // This constructor will get executed for 3rd kind of object
   {
      System.out.println("2 arguments constructor");
   }

   {                                                  // Creation of an instance block
      System.out.println("Instance block");          
   }

   public static void main(String[ ] args)
   {
      new Test( );                                      // Object of 1st kind
      new Test(10);                                     // Object of 2nd kind
      new Test(10,20);                                  // Object of 3rd kind
   }
}

OUTPUT-

Instance block
0 argument constructor
Instance block
1 argument constructor
Instance block
2 arguments constructor

 

It can be clearly observed from the above program that whenever any object gets created, first of all instance block is executed and then its specific constructor is executed.

 

Multiple instance blocks-

 

  • It is possible to have multiple instance blocks inside a single class.
  • The execution of these multiple instance blocks take place from top to bottom.

 

Program illustrating the use of multiple instance blocks-

class Test
{
   Test( )
   {
      System.out.println("0 argument constructor");
   }

   Test(int a)
   {
      System.out.println("1 argument constructor");
   }

   {
      System.out.println("Instance block-01");
   }

   {
      System.out.println("Instance block-02");
   }

   public static void main(String[ ] args)
   {
      new Test( );
      new Test(10);
   }
}

OUTPUT-

Instance block-01
Instance block-02
0 argument constructor
Instance block-01
Instance block-02
1 argument constructor

 

Important Points to note-

 

  • Instance block will be executed only once for each object during its creation.
  • So, the number of times an instance block is executed signifies the number of objects created in the program.
  • The execution of instance block depends only on the object creation and not on the execution of a constructor.

 

Comparison between instance block and constructor-

 

Instance block Constructor
Instance block logic are common for all the objects. Constructor logic are specific to the objects.
Instance block will be executed only once for each object during its creation. We can execute the constructors multiple times for a single object by placing the call to them in other constructors.
Instance block will be executed only as many times as there are number of objects created in the program. It is not necessary for the constructors as they may be called explicitly.
Instance blocks are mainly used for writing the common logic which we want to execute for all the objects. Constructors are mainly used for the initializing of objects.

 

Get more notes and other study material of Core Java.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Summary
Instance Blocks in Java
Article Name
Instance Blocks in Java
Description
Instance block is an element of a class which serves the same two purposes as served by the constructors. Instance block will be executed only once for each object during its creation.
Author
Publisher Name
Gate Vidyalay
Publisher Logo

Spread the love