Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic