wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain the initialization sequence in this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain the initialization sequence in this code" Watch "Please explain the initialization sequence in this code" New topic
Author

Please explain the initialization sequence in this code

nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
Can somebody tell me how output "null" is produced by this program? i thought answer is child but it is null.. how?

-----------------------------------------------------------------
Regards,
nitin

(added tags and clarified topic title)
[ July 21, 2006: Message edited by: Barry Gaunt ]
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
The instance variable 's' in the child is initialized to "Child" after the superclass constructor runs. However, they are zero-initialized (i.e. references initialized to null) before the superclass constructor runs.

This explains why output shouldnot be "Child".
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
how do i forget this?

thanks buddy for your help.

regards,
nitin
ram gaurav
Ranch Hand

Joined: Mar 29, 2006
Posts: 208
Sir i have one confusion , like when in Parent constructor mathord() is called , then according to me it should be called of Parent but its calling of Child , can you please explain why this is happening.

Thanks
Gaurav
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
instance methods are dynamically bound. In other words, the exact method which is dispatched depends on the actual object referenced by "this" reference. Note that constructors are no exception to this, and the methods referenced from the constructors are also dynamically bound. Thus, when the Parent() constructor is called through Child() constructor, the method() call, though done from the Parent constructor, is resolved to Child.method() and not Parent.method().
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by ram gaurav:
Sir i have one confusion , like when in Parent constructor mathord() is called , then according to me it should be called of Parent but its calling of Child , can you please explain why this is happening.

Thanks
Gaurav


Check out what overriding means and understand what happens when you call an overridden method in a base class. The call method() in the base class is the same as this.method() where this is a reference to the base class object.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
GANESHKUMAR Parthsarathi
Greenhorn

Joined: Jan 11, 2006
Posts: 13
The flow order after new is

1) Call the constructor
2) Call Super constructor if any(here parent)
3) Initilize static & instance variables(S = "parent")
4) Execute the statements in the constructor(call to method... This referees the method in the child. Not the method in the Parent.)


So when the method in the child is called the variable "S" is yet to be initilized. So "null" is printed.

If u make the the string in child as static, then u can have "child" O/p.

Cos static will be loaded at the time of loading the class.

Since the method is overridden in the child class the overridden method will take the higher priority.


Correct me if i'm wrong.
[URL=Order for Object Creation]
[ July 21, 2006: Message edited by: Barry Gaunt ]
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
Yes. You are right, Ganeshkumar.

Static initializations (and initializer blocks) are processed before the main() method is called. So, of course, the String would be already initialized.

Very good. You seem to understand it very well.


SCJP 1.4 - 95%
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Object Initialization Sequence
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain the initialization sequence in this code
 
Similar Threads
Please help explain the output
Help
a question from teamtesting
Constructor doubt
Constructor,why displaying null on this program