• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calling instance methods

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cannot call instance methods until after super constructor runs.


Then why does this code give the output 0,4
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic