aspose file tools*
The moose likes Java in General and the fly likes Calling an object's non-static method before its constructor has run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling an object Watch "Calling an object New topic
Author

Calling an object's non-static method before its constructor has run

Dilbert Adams
Greenhorn

Joined: Nov 13, 2003
Posts: 3
If we are creatinga child that extends a parent, and both of them have a common (=overridden) method named CommonMethod,
this is what I think should be the sequence (shown in the code below):
1) JVM starts by starting the instantiation of Child
2) Since Child extends Parent, instantaition of Child is temporarily suspended
3) JVM starts the instantiation of Parent
4) Constructor of Parent is called
5) Parent's Constructor calls the method CommonMethod. This method exists in both Child and Parent
6) Here is the crux. I think that Parent's CommonMethod should be called. Execution however showed that Child's method was called instead!
7) And THEN, JVM instantiates (rather, completes the instantiation process of) Child

BUT: When I executed the below code , this is what happened:
(A) Parent constructor did get called first - as you would expect
(B) CommonMethod called used the "semi-existent" Child object's CommonMethod, not Parent's !
(C) AND THEN the constructor for the child object was called

Can someone please point out the error in my concepts?


public class Tester
{
public static void main(String[] args)
{
Child c = new Child() ;

}
}

class Parent
{
public Parent() { CommonMethod() ; }
public void CommonMethod() { System.out.println ("I am the Parent") ; }
}

class Child extends Parent
{
public Child() { CommonMethod() ; }
public void CommonMethod() { System.out.println ("I am the Child") ; }
}

So I expected that the output would be:
1) I am the Parent
2) I am the Child

The actual output was
1) I am the Child
2) I am the Child

Need to understand the 'real sequence of events', I guess


Scan My Thumbprint :-|
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39


First of all, there is only *ONE* object being instantiated -- not two. A child IS-A parent, the parent constructor is being called to instantiate the portion of the child that it inherited from the parent -- no separate object is being created.

To answer your question, polymorphism applies -- the overriden version of the method is always called. Even when instantiation of the object has not yet completed. This means that you have to be careful when calling methods during construction, as it is possible that the object is not yet in a ready state.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

Dilbert Adams wrote:
Need to understand the 'real sequence of events', I guess


Step Zero. Not really a step. Instance variables that are compile time constants are already instantiated. So, you can say they are done first (although technically they are constants and not variables).

Step One. During instantiation of a class, the super constructor is called first. This actually leads to that constructor's super constructor being called, and so on.

Step Two. Upon completion of the super constructor, the initialization of instance variables, and instance initializers are executed in the order that they are encountered in the source code.

Step Three. The rest of the constructor, after the super constructor call is done.

Henry
Dilbert Adams
Greenhorn

Joined: Nov 13, 2003
Posts: 3
Thanks Henry. I was going wrong by not realizing that just like an instance var is initialized *before* the constructor is called, a method is actually just like a variable. Therefore it IS available before the constructor call is over !

Perfect - thanks again !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling an object's non-static method before its constructor has run
 
Similar Threads
Method Overlaoding questions/doubt
Doubt in Mock Question
Question for variable initialization order
Overriding static methods
Reg. invoking private methods