This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor,why displaying null on this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor,why displaying null on this program" Watch "Constructor,why displaying null on this program" New topic

Constructor,why displaying null on this program

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 481

When the Child class loaded in main() method,at that time s=�Child� member variable is initialized.Right?
Then why the answer s=null printing when we access super class constructor method() will call Child class method() method?I thought it would print s=Child.Please Explain the rules.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Because the superclass is initialized during the process of initializing the subclass. The instance variables in the subclass are given default values, and before they are initialized there is an implicit call to the no-args constructor of the superclass. During that call, an overridden method is called. Since it is overridden in the subclass and that is the method that executes. Since the subclass has not finished initializing yet, the value of its instance variables are the default values.
[ September 20, 2006: Message edited by: Keith Lynn ]
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257


The simple & straight answer is, the instance variables are created and assigned default values until the super class constructor is executed, then the values are assigned.


Inside this Parent Hello
Inside Method Child null
Inside this Child Hello
Inside Method Child Hello

As you see, the Parent method() is not called....
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 481
Srinivas and Keith thank you so much for all your help.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Constructor,why displaying null on this program
It's not a secret anymore!