Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question for variable initialization order

 
avseq anthoy
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think the output is
Parent,Parent
Child ,Child.
But the real output is
Child, null
Child,Child

I think the class initialzation order is :
1. the static variable
2. super's member variable
3. super's constructor
4. child's member vaiable
5. child's constuctor

Are there any thing wrong for the order?
I think the parent's constructor will be call before the child's constuctor , so the showName function in the Parent will be call fisrt.
But why the showName function in Child will be call first , although the function is override in the Child class.
Can somebody explain it to me why I think wrong.

Tks in advance
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that when showName is called in Parent's constructor, it is calling the overridden showName() method of class Child.

Your initialization order is right. However, Child's showName is already called at step 3 (because it overrides Parent.showName) before name is initialized at step 4. That's why it's null.

We've had a similar thread about this just this week, either here or in our Java in General forum. Maybe you can find and read that one too.
 
avseq anthoy
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tks for your reply.
But I was confused that when the parent's constructor was called , the child class has not initialized.
Why the parent's constructor can call the child's method?


Rob Prime wrote:The problem is that when showName is called in Parent's constructor, it is calling the overridden showName() method of class Child.

Your initialization order is right. However, Child's showName is already called at step 3 (because it overrides Parent.showName) before name is initialized at step 4. That's why it's null.

We've had a similar thread about this just this week, either here or in our Java in General forum. Maybe you can find and read that one too.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avseq anthoy wrote:tks for your reply.

Please UseRealWords: "thanks".

But I was confused that when the parent's constructor was called , the child class has not initialized.
Why the parent's constructor can call the child's method?

Because the method is overridden, and all polymorphism rules still hold, even when a method is called from a constructor. That's why it's a bad idea to call a method that can be overridden from a constructor. If you ever do this then document it, so if a sub class is created the developer knows not to rely on fields being instantiated.
 
avseq anthoy
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your reply. Thanks for your enthusiasm.
The reply is very helpful.

Rob Prime wrote:
avseq anthoy wrote:tks for your reply.

Please UseRealWords: "thanks".

But I was confused that when the parent's constructor was called , the child class has not initialized.
Why the parent's constructor can call the child's method?

Because the method is overridden, and all polymorphism rules still hold, even when a method is called from a constructor. That's why it's a bad idea to call a method that can be overridden from a constructor. If you ever do this then document it, so if a sub class is created the developer knows not to rely on fields being instantiated.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for "beginning". Moving thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic