This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes More on Statics in super() & Initialization Order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "More on Statics in super() & Initialization Order" Watch "More on Statics in super() & Initialization Order" New topic
Author

More on Statics in super() & Initialization Order

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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... ...


BEE MBA PMP SCJP-6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Jim Hoglund wrote:Would instanceVar have its default value (or null)?


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

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: More on Statics in super() & Initialization Order