Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Rules for Constructors

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In K&B, it says
"You cannot make a call to an instance method, or access an instance variable,until after the super constructor runs."

What's the meaning of this? Can someone explain it with a code? Does this mean you can't use instance variable/methods in a constructor?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The use of Super or this must be the must line in constructor.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not true, and unfortunately, fallacy is rife in a lot of technical books.

However, my guess at what it was trying to say is that the first statement in a constructor must be a call to a superclass constructor or 'this' constructor, otherwise, a compile-time error results.

Here is an example that directly contradicts the statement made by your book:
http://qa.jtiger.org/GetQAndA.action?qids=10&showAnswers=true
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You CAN make call to an instance method and use instance variables. However, the instance variables of the subclass are not intialized until the constructor of the subclass is reached.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method super() or this() is the first statement in a constructor. If you call super() method other than first statement it show an error. using this()/ super() operator we can overload the super class or derived class constructor.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
super or this is the first line in every constructor, either implicitly or explicitly.

What that statement in K&B means is that no instance members is available until the super call to superclass has completed. In other words, until all the constructors in the inheritance tree have completed. Until then you will be able to use safely instance members in the constructors.

Look at this example:



If i is declared static, then it will be fine.

Regards,
Francisco
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic