Initialization block in JAVA

Discussion in 'Java' started by shabbir, Aug 23, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    An initialization block is a block of code between braces that is executed before the object of the class is created. As the execution of the initialization block is dependent on the creation of the object we can easily make a guess that it has two types of object.

    1. Non static initialization block.
    It is dependent on the object and the initialization block is executed for each object of the class that is created. It can initialize instance member variables of the class. Here is a simple example.
    Code:
    class InitializationBlock 
    {
    	int[] arrVal = new int[10];
    	// Initialization block
    	{
    		System.out.println("In initialization block\n");
    		for(int i=0; i<arrVal.length; i++)
    		{
    			arrVal[i] = (int)(100.0*Math.random());
    		}
    	}
    
    	void Display()
    	{
    		System.out.println("Start display\n");
    		for(int i=0; i<arrVal.length; i++)
    		{
    			System.out.println("  " + arrVal[i]);
    		}
    		System.out.println("End display\n");
    	}
    
    	public static void main(String[] args) 
    	{
    		InitializationBlock ib1 = new InitializationBlock();
    		System.out.println("First object is created");
    		ib1.Display();
    
    		InitializationBlock ib2 = new InitializationBlock();
    		System.out.println("Second object is created");
    		ib2.Display();
    	}
    }
    
    2. Static initialization block.
    It is defined using the keyword static and is executed once when the class is loaded and has a restriction that it can only initialize static data members of the class.
    Code:
    class InitializationBlock 
    {
    	static int[] arrVal = new int[10];
    	// Initialization block
    	static 
    	{
    		System.out.println("In initialization block\n");
    		for(int i=0; i<arrVal.length; i++)
    		{
    			arrVal[i] = (int)(100.0*Math.random());
    		}
    	}
    
    	void Display()
    	{
    		System.out.println("Start display\n");
    		for(int i=0; i<arrVal.length; i++)
    		{
    			System.out.println("  " + arrVal[i]);
    		}
    		System.out.println("End display\n");
    	}
    
    	public static void main(String[] args) 
    	{
    		InitializationBlock ib1 = new InitializationBlock();
    		System.out.println("First object is created");
    		ib1.Display();
    
    		InitializationBlock ib2 = new InitializationBlock();
    		System.out.println("Second object is created");
    		ib2.Display();
    	}
    }
    
    The only difference between both the samples above is with the two static keywords added and you can definitely see difference in the output. One outputs different values for two objects created and other one the same values.

    You can have multiple initialization blocks in a class in which case they execute in the sequence in which they appear in the class.

    Note that any initialization block present in the class is executed before the constructor.

    So now the question comes why we need constructors if we have initialization blocks. The answer is we don't need the default constructor but initialization block cannot be parameterized and so you cannot have objects taking values from out side and so initialization block is not a substitute for constructor.
     
  2. Balaji Reddy

    Balaji Reddy New Member

    Joined:
    Mar 23, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thanks shabbir . very helpful
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice