This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initialiser block/constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initialiser block/constructor" Watch "initialiser block/constructor" New topic
Author

initialiser block/constructor

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Which one is executed first?
Initialiser block or constructor and why?


In this code ..the first line of the base constructor is executed whihc leads to the second constructor whihc in turn calls hte base class constructor.
Why does it then execute the innitiliser block instead of the base class constructor.
Also,does the constructor run before initailsing the instance varables or after it?


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Nabila Mohammad:
...Why does it then execute the innitiliser block instead of the base class constructor. Also, does the constructor run before initailsing the instance varables or after it?

Your second question is part of the answer to your first question.

This is all part of the initialization that occurrs before the body of the constructor executes.

Initializer blocks execute and instance variables are initialized in the order they appear in the code, and this all happens before the body of the constructor executes. This allows the constructor to use initialized instance variables.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Try predicting the output of this code, then compile and run to check it...
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hey Marc..
Thanks Alot..
That was a great program to understand how line by line execution works.
Thanks..you have been a Biiigg help!
Take Care.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I got Most of it Right..
except the first line which I thought would be executed first.
Thnks.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I still have a small problem...
I excuted another program just to chech the step of execution...
Noticed that..
first all the static variables and blocks get executed according the order in whihc they are written.
Then when the objects are being created the Parent class constructor is first run followed by the initialisng of the non static varibles and block in the order in which they are written
And after that..the base class constructor is run.

i dint get it?
Why is the parent class constructor being run first and not followed by the base class.
or why dint the variables get initialised before any of the constructor is run.
Why only parent class constructor?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Take a look at the output from this code...
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
ok..
so first Static blocks are executed Superclass and then base class..
Then Superclass non static block and constructor.
and the base class non static block and constructor.
Can you tell me why is it executing this way...
i dint get they Why part of the question..
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Why is the parent class constructor being run first and not followed by the base class.

If you investigate sub- class's Constructor, you will find that it it has an implicit call to super- class's Constructor as the very first statement.

After super- class's Constructor finishes, instance variables/blockes got initialized, finally Constructor's body got executed.

From marc sample:
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Nabila Mohammad:
...Can you tell me why is it executing this way...
i dint get they Why part of the question...

Consider that a Subclass IS-A Superclass. So the first step in creating a Subclass is to create a Superclass, and then "extend" that Superclass to make it a Subclass.

First, we need the blueprint -- the class itself. The Superclass class initializes first, with static variables and static initializers. When this is done, there is a fully initialized Superclass class to work with, and so the Subclass class can initialize its own static variables and static initializers.

After the class (the blueprint) is fully initialized (from the top down), then we start creating the object.

Again, a Subclass IS-A Superclass, so we start by creating an instance of Superclass, initializing instance variables and executing non-static initializers. When this is done, the Superclass constructor can run (using initialized instance variables, if needed). At that point, there is a fully formed Superclass object to work with. The next step is to initialize the Subclass instance (instance variables and initializers), and finally execute the Subclass constructor.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks Alot..
You explain well.
Good Going! Tc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initialiser block/constructor
 
Similar Threads
constructor initialize order
Object initialization
Inheritance Doubt #1
Inherited Constructor invocation
Inheritance again