Posted By:
Christopher_Schultz
Posted On:
Monday, December 24, 2001 09:10 AM
I think this article may have your answer:
http://www.javaworld.com/javaworld/jw-11-2001/jw-1102-java101_p.html
If you take a look at the way Java initializes instances, you'll find that even though the subclass's constructor gets called first, the superclass's constructor actually executes first. This is because the first action in a constructor must be a call to its superclass constructor.
The Java compiler executes all of the instance initialization for your class Sub after Super. executes. That means that after Super. calls Sub.init and sets s to "Hello", your Sub constructor clobbers that value with null.
The problem here is that you are making the compiler do one type of initialization (explicit init to null in your declaration), and the runtime is 'initializing' the attribute to "Hello". Due to the way the compiler handled its initialization of object instances, your runtime initialization failed.
Moral of this story: don't use declarative initializers unless you really want to declare that attribute to have that value.
Hope that helps,
-chris