| Author |
constructor
|
Shiva Mohan
Ranch Hand
Joined: Jan 05, 2006
Posts: 465
|
|
i though line 1 would give error like this.f cannot reference before this(f) exceution.but it executed fine and printed f=0.0 .how?
|
 |
megha joshi
Ranch Hand
Joined: Feb 20, 2007
Posts: 206
|
|
Hi Remember the cycle... First the static code blocks run. Second the instance variables are given default values always After that the constructor and super constructor runs and then the instance variables are initialized to any values provided in construtors or initialization blocks. The default value of instance variable f which is float is 0.0. So its given that value and when the constructors executes...it prints the default value.
|
 |
Shiva Mohan
Ranch Hand
Joined: Jan 05, 2006
Posts: 465
|
|
Thanks meg. Just now only i have known that
Second the instance variables are given default values always.
Thanks again for the help.
|
 |
Shiva Mohan
Ranch Hand
Joined: Jan 05, 2006
Posts: 465
|
|
One more doubt. .why the 0.0 default value is not passing here?line 1 is showing compile error.
|
 |
Anton Uwe
Ranch Hand
Joined: Jan 10, 2007
Posts: 122
|
|
|
"f" does not exist at the moment you try to use. First, the (implicit) call (chain up to Object) to super() must be executed. Then f exists initialized with the default value.
|
 |
megha joshi
Ranch Hand
Joined: Feb 20, 2007
Posts: 206
|
|
Sorry I need to modify what I said earlier... First static code blocks At this point only static variables can be used. Second the default call to this() or super() or the programmer inserted call to this()(args optional) super() keeps on calling super() until the supermost object() is called. At this point instance variables get their default values. At this point instance variables can be used Then the rest of the code in the constructor runs And then initialization blocks You can find the above in K&B book.
|
 |
Shiva Mohan
Ranch Hand
Joined: Jan 05, 2006
Posts: 465
|
|
As anton said,i was expecting this.f cannot reference before this(f) exceution.But this.f is executed fine and gave resul 0.0 before finished this(5) execution.is the above program flow of execution different from the below one. this program gave me the compile error at line 1 as expected.But i am asking how the first program is executing without an error.Please help me.
|
 |
Anton Uwe
Ranch Hand
Joined: Jan 10, 2007
Posts: 122
|
|
The sniplet from your first code is the same as So, because super() was already executed before "line 1", "f" already exists with its default value.
|
 |
anil kumar
Ranch Hand
Joined: Feb 23, 2007
Posts: 447
|
|
hi good question. See in the first question you are this(5).Here you are epassing a literal. To an overloaded constructor.From there the super class constructor is beging called.After it will come to System.out.println(this.f);//line 1 this statement.Here replace that statement to this statement. now we can assign any value to instance variable . so try to assign this.f=10; after execution of this statement the control will go to default constructor.Here if you want you can print. ONE THING KEEP IN MIND THAT YOU CAN ASSIGN OR YOU CAN A USE(LIKE YOU DID IN THE LAST EXAMPLE OF THE PAGE) INSTANCE VARIABLE AFTER SUPER CLASS CONSTRUCTOR IS EXECUTED . OK in last example code you did like this .this(f).At this point the super class constructor is not called.That is why you are getting error. [ February 27, 2007: Message edited by: anil kumar ]
|
 |
Shiva Mohan
Ranch Hand
Joined: Jan 05, 2006
Posts: 465
|
|
|
Thank you very much Anton and Anil.I got it.
|
 |
 |
|
|
subject: constructor
|
|
|