| Author |
Question for variable initialization order
|
avseq anthoy
Ranch Hand
Joined: Apr 27, 2004
Posts: 102
|
|
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
|
My Way,My Pace
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
avseq anthoy
Ranch Hand
Joined: Apr 27, 2004
Posts: 102
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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
Joined: Apr 27, 2004
Posts: 102
|
|
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
Joined: Oct 13, 2005
Posts: 32712
|
|
|
Too difficult a question for "beginning". Moving thread.
|
 |
 |
|
|
subject: Question for variable initialization order
|
|
|