• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Again with contructors

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Corey
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic