Well, back to JAVA
Hmm, so an Initialization Block – the name itself suggests that it has something related to initialization – of what? Of Objects.
Of Objects because whenever we create an object in Java using “new” keyword the constructor is called and basically constructor is meant for initialization of instance(member)(Object) variables.
Let’s take a look at a very simple example
class Parent
{
static { System.out.println(“Parent static 1″);} //#1
{ System.out.println(“Parent Instance Block 1″);} //#2
static { System.out.println(“Parent static 2″);}
{ System.out.println(“Parent Instance Block 2″);}
Parent()
{
System.out.println(“Parent constructor”);
}
}
class InitializationBlocks
{
public static void main(String args[])
{
Parent p1=new Parent();
}
}
Now look above we have some blocks of code…
Some are marked static and some are simply lying with no keyword attached to them…
The one marked static (see comment #1) are Static Initialization Blocks, they are loaded
when the class is loaded. Well, you can use them to initialize your static variables. Nice na?
And the one marked …marked? Have I marked them? (Oh c’mon see comment #2) those are Instance Initialization Blocks, they are basically called when you create objects of your class. Suppose if you want a similar work to be done for all the objects then you can put it in Instance IB.. Just Try It..!!
Static IB will be called only once when the class is loaded and Instance IB will be called every time when you create an object of your class.
And finally Constructor will be called.
Hey why don’t you look at the output of the above program?
Parent static 1
Parent static 2
Parent Instance Block 1
Parent Instance Block 2
Parent constructor
Now what if we have an inheritance tree…
In that case …do you know how the constructors are called in case if you have an inheritance tree??
grandparent, parent and then child…m I rite?
So just remember this sequence…
First grandparent will be loaded then parent and then child will be loaded ….so
Static IB will run…
Then Instance IB of grandparent followed by constructor of grandparent
Next Instance IB of parent followed by constructor of parent
Finally, Instance IB of child then constructor of child
Here is the code..
Have a look..
//Initialization Blocks
class GrandParent
{
GrandParent()
{
System.out.println(“GrandParent constructor”);
}
static { System.out.println(“GrandParent static 1″);}
{ System.out.println(“GrandParent Instance Block 1″);}
static { System.out.println(“GrandParent static 2″);}
{ System.out.println(“GrandParent Instance Block 1″);}
}
class Parent extends GrandParent
{
Parent()
{
System.out.println(“Parent constructor”);
}
static { System.out.println(“Parent static 1″);}
{ System.out.println(“Parent Instance Block 1″);}
static { System.out.println(“Parent static 2″);}
{ System.out.println(“Parent Instance Block 2″);}
}
class Child extends Parent
{
Child()
{
System.out.println(“Child constructor”);
}
static { System.out.println(“Child static 1″);}
{ System.out.println(“Child Instance Block 1″);}
static { System.out.println(“Child static 1″);}
{ System.out.println(“Child Instance Block 2″);}
}
class InitializationBlocks
{
public static void main(String args[])
{
Child c1=new Child();
}
}
Ok so what will be the output its simple right?
Isn’t it?
If you have understood this topic then you can put the output in comment…please do this
favour so that I can save my keystrokes….lol
Regards,
Faraah Dabhoiwala.
Posted by kre alkalyn on October 10, 2009 at 3:55 PM
Thanks buddy since I am working on Java right now in some research work where I run my program and it is not compiling perfectly so I am little bit confuse .
When I am come across your blog I find my solution so great going.
Posted by Faraah & Shadab on October 31, 2009 at 11:48 AM
Thanks for your appreciation.
Regards,
Faraah & Shadab