This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons 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: 219



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: 3605
    
  14

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.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219


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: 3432
    
  12
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: 4344
    
    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: 219

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in Constructor Chaining
 
Similar Threads
For the Nth Time Java Pass By copy?
compilation error
supertype constructor
static block and constructor problem
[Exercise] Prove that base class constructor is always called