• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Please explain the initialization sequence in this code

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do i forget this?

thanks buddy for your help.

regards,
nitin
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object Initialization Sequence
 
Get me the mayor's office! I need to tell him about this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic