• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain the initialization sequence in this code

 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i forget this?

thanks buddy for your help.

regards,
nitin
 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
GANESHKUMAR Parthsarathi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object Initialization Sequence
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic