• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more doubt.



.why the 0.0 default value is not passing here?line 1 is showing compile error.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Anton and Anil.I got it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic