This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes calling instance methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "calling instance methods" Watch "calling instance methods" New topic
Author

calling instance methods

K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
Cannot call instance methods until after super constructor runs.

Then why does this code give the output 0,4
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
From the point where you create object (a), the constructor for class A
invoked the print() method.We should now that methods are resolve at runtime and instance variable during compilation,
so the print() method of class B is invoked accsessing variable i, and at that time, variable i
was not yet initialized so the default value was 0 (it is an instance variable).

After object (a) was created, its print() method was invoked (because methods are resolve at runtime)
thus the value was rounded and prints 4.

arnel
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
Originally posted by arnel nicolas:
From the point where you create object (a), the constructor for class A
invoked the print() method.We should now that methods are resolve at runtime and instance variable during compilation,
so the print() method of class B is invoked accsessing variable i, and at that time, variable i
was not yet initialized so the default value was 0 (it is an instance variable).

After object (a) was created, its print() method was invoked (because methods are resolve at runtime)
thus the value was rounded and prints 4.

arnel


Thanks arnel
What i wanted to know was why is the print method called when super constructor has not completed.
Page 316 K&B
You cannot make a call to n instance method or access an instance variable until after the super consructor has run


But in this case when B's print method is called from within A's constructor, super constructor of B that is A has not yet compeleted. Why doesn't the above statement apply here?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I think the statement read be "should not" rather than "cannot". It's clearly possible and it works as you can see by running the example.

You "should not" do it because it makes the construction of the super class rather unpredictable. When you write such a base class and write a constructor for it you should restrict the member methods that it calls to being private. In that way you prevent subclasses overriding the method and producing unpredictable (for you, the base class provider) results.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling instance methods