Friday 30 September 2016

Initialize Blocks and Class Initialize Java

 Discuss Initialize Blocks and Class Initialize blocks.



 First of all, there are two types of initialization blocks: 


1. Instance Initialization Blocks (Initialization Blocks)
2. Static Initialization Blocks (Class Initialization Blocks).



1. Initializer block


In a class definition, we can have a member block with no name. Such a block is known as the initializer block.

An initializer block is never invoked by any application directly since it does not have any name. However, it is always invoked on an instance as soon as it is allocated.

It is invoked just before the constructor is invoked on the instance. 




Rectangle(int l, int w)
{
 // remove the increment of count from the constructor
setDimensions(l, w);
}
{
 // initializer block, executed whenever any instance is
created.
count++;
 } 


The initializer block in a class is invoked whenever any of the constructor for the class is invoked. This block is always executed before the constructor code is executed.

The initialization process for an instance is thus executed in the following sequence. Whenever any constructor is invoked with the new operator to create a new instance, first the space is allocated for the instance depending on the instance variables declared in the class.

When this space is allocated, its instance variables have the default initial valueaccording to the type of variable, i.e. numeric types will be 0, boolean types will be false and reference types will be null; now in case the instance variable declaration has an assignment, then such an assignment will be executed on the instance; for example, in caseof the Rectangle class, if the instance variable length were declared as

int length = 7; // instance declaration with assignment


then the assignment length = 7 would be executed initially. Now after executing instance variable initialization,  the initializer block would be executed, and then it is followed by execution of the specific constructor code that is invoked.





2. Class Initializer Block

Just like we have the constructor for initializing the instance variables, we use the class initializer block to initialize the class variables.

A class initializer block is created just like the initializer block, but it is declared to be static. We also call this block as the static block.

We also call this block as the static block, e.g. we may declare a static block as given

below...


class Rectangle
{
                static int count;
                static
                {
                                count = 0;
                                System.out.println("Inside a class initializer
                                block");
                }
}



Just like the initializer block, the class initializer block does not have a name and is 
not invoked directly by an application. This block is automatically executed whenever the 
class is loaded.


A class is normally loaded only once at the runtime. The class would be loaded by 
the JVM only if the class is used in an application.

Difference between Initialize Block & Class Init Block


The static block is only loaded when the class object is created by the JVM for the 
1st time whereas init {} block is loaded every time class object is created. Also first the static 
block is loaded then the init block. 






public class LoadingBlocks {
          static
          {
                   System.out.println("Inside static");
          }
          {
                   System.out.println("Inside init");
          }
          public static void main(String args[])
          {
                   new LoadingBlocks();
                   new LoadingBlocks();
                   new LoadingBlocks();
          }
}
Output:
          Inside static
          Inside init
          Inside init
          Inside init







This code should illustrate in which order they both are executed
public class Test
{
                 static int staticVariable;
                 int nonStaticVariable;
                 // Static initialization block:
                 // Runs once (when the class is initialized)
                 static
                 {
                                                System.out.println("Static initalization.");
                                                staticVariable = 5;
                 }
                 // Instance initialization block:
                 // Runs each time you instantiate an object
                 {
                                 System.out.println("Instance initialization.");
                                 nonStaticVariable = 7;
                 }
                 public Test()
                 {
                                System.out.println("Constructor.");
                 }
                 public static void main(String[] args)
                 {
                                 new Test();
                                 new Test();
                 }
}
Output :
                Static initalization.
                Instance initialization.
                Constructor.
                Instance initialization.
                Constructor.




IN SHORT :

Initializer block is used to define the activity that is required to be carried out whenever any instance is created for the class. There can be any number of initializer blocks in a class definition. These would be combined and treated as one initializer block. This code is executed just before any of the constructor code is executed. 

Class initializer block is used to initialize the class variables. This block is executed only once when the class is loaded, and similar to the initializer block in a class definition we can have any number of class initializer blocks, which would be combined and treated as
one.

No comments:
Write comments

Labels

ADVERTISEMENT