Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More on Statics in super() & Initialization Order

 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an earlier post, Rob Prime offered this code to illustrate limits on
super() parameters during construction. I have a follow-on question.Are statics okay and others not because of initialization order? Is the following
true? Statics are set up as each class is loaded (top-to-bottom in the code),
starting with the loaded (lowest) class, then its super class and on up toward
Object. Then initialization continues from the most super class on down. In each;
class parameters, then initialization code blocks followed by constructor code.

Jim... ...
 
Henry Wong
author
Marshal
Pie
Posts: 20999
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Hoglund wrote:Are statics okay and others not because of initialization order? Is the following
true? Statics are set up as each class is loaded (top-to-bottom in the code),
starting with the loaded (lowest) class, then its super class and on up toward
Object. Then initialization continues from the most super class on down. In each;
class parameters, then initialization code blocks followed by constructor code.


Statics are obviously allowed because they are initialized upon loading, which should be earlier than instantiation.

If I had to guess why instance variables are not allowed, it was because instance variables are initialized after the super() call, so this was supposed to guarantee that the instance variable be initialized before usage. Unfortunately, this doesn't work as it is possible for super classes to call methods of the sub class during construction, when instance variables are not yet initialized.

Henry
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A small tweek...

So you can not pass an instanceVar in a super() call, but super() can turn
around and call an overridden method that relies on the same instanceVar,
before it's been initialized.

Would instanceVar have its default: 0, 0.0, \u0000 or null?

Jim ... ...
 
Henry Wong
author
Marshal
Pie
Posts: 20999
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Hoglund wrote:Would instanceVar have its default value (or null)?


The default for references is null. So... yes.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic