File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Question for variable initialization order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question for variable initialization order" Watch "Question for variable initialization order" New topic
Author

Question for variable initialization order

avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104


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: 19783
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
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: 19783
    
  20

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: 104
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: 40038
    
  28
Too difficult a question for "beginning". Moving thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question for variable initialization order