• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question from teamtesting

 
hua chen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic