Monday, December 24, 2001 09:10 AM
I think this article may have your answer:
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
Super. executes. That means that after
Sub.init and sets
Sub constructor clobbers that value with
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,