Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initialiser block/constructor

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try predicting the output of this code, then compile and run to check it...
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got Most of it Right..
except the first line which I thought would be executed first.
Thnks.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the output from this code...
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alot..
You explain well.
Good Going! Tc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic