aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question from teamtesting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question from teamtesting" Watch "a question from teamtesting" New topic
Author

a question from teamtesting

hua chen
Greenhorn

Joined: Aug 18, 2006
Posts: 8
hello all,

I got a question from teamtesting as following:


the answer is null. can anybody tell me why?

thank you very much.
[ August 28, 2006: Message edited by: Barry Gaunt ]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

You gave us some code, but didn't tell us what the question is.

What was the question? Now I'm curious.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Because the assignment of explicit value to s of Child will be after the execution of all super class constructor.

You have called instance method method() of Child class from Parent() which in turn calls instance variable s of Child. Since still Parent class constructor's execution is not completed, so printing s will return null.

Calling method() in Child constructor will give you the explicit assigned value.



Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
In the main method, when you are instantiating a child object, the child constructor will be called first whose first line calls the constructor in super class. Now, the String reference variable in Child class is not initialized yet since the constructor has not completed. Now the method() is called (which is the method in the Child class). This will print the default value for a field which is null in this case. After all this, the constructor completes giving the variable the value.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question from teamtesting