• 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

Again with contructors

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code from Prof. K's book:

The code above gives the output:
Creating an object of SubclassB.
Constructor in SuperclassA
value: 0
Constructor in SubclassB
value: 800
superValue: 0

MY QUESTION: I thought that member variables are initialized at class load time which is before the call to the constructors. The code above, seen from output, shows that value was not initialized to 800 before this.doValue() of SuperClassA was called. How is that so?

Edited by Corey McGlone: Added CODE Tags
[ June 22, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Member variables are initialized to 0 when the class is initially instantiated. It won't be set to 800 until the proper instance member initializer is executed. If you look at your other post (where I cite the steps the JLS lays out), you'll notice that the instance member initializers of the subclass don't execute until after the constructor of the superclass executes completely.

Also, it's important to note that the method invocation in your superclass actually invokes the method in the subclass because the method is overridden. The method doValue in the superclass is actually never executed.
 
Pal Sudarshan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey
reply
    Bookmark Topic Watch Topic
  • New Topic