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 Problem in Constructor Chaining Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem in Constructor Chaining" Watch "Problem in Constructor Chaining" New topic
Author

Problem in Constructor Chaining

Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 225



i am getting a compilation error in the above code. It is a rule that a superclass constructor must run before any sub class. In the above code the super class constructor is called by the one argument constructor in which the compiler adds "super()" by default. right?? if yes then why am I getting error like:

javac Cert.java
Cert.java:8: cannot reference x before supertype constructor has been called
public Grade(){ this(x); x++;}
^

can please someone explain me the flow how this happes..?

OCPJP
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3884
    
  18

Let's start with proper code formatting:
The problem lies with the no-arg constructor Grade(). You're calling this(x). What value are you passing? Remember that the object isn't initialized yet, because it hasn't finished chaining constructors.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 225


Well stephan i am still not clear with it.. i didnt called the no-argument constructor at all then why is it giving me error on that part.. when does that no argument constructor gets called??
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Ishan Pandya wrote:

Well stephan i am still not clear with it.. i didnt called the no-argument constructor at all then why is it giving me error on that part.. when does that no argument constructor gets called??

The compiler doesn't know that the no-arg constructor is not called. It might be called from another class. Therefore the compiler has to ensure the code is valid.
If you don't need the no-arg constructor, why did you put it in ?


Joanne
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4536
    
    8

And just because nothing calls the no-arg constructor now, it doesn't mean nothing will in the future. You can't have new classes being written that suddenly breaks an existing class because they use a method or constructor that isn't valid.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 225

Matthew Brown wrote:And just because nothing calls the no-arg constructor now, it doesn't mean nothing will in the future. You can't have new classes being written that suddenly breaks an existing class because they use a method or constructor that isn't valid.


I am clear with it.. thankyou so much.
 
jQuery in Action, 3rd edition
 
subject: Problem in Constructor Chaining
 
It's not a secret anymore!