This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in Constructor Chaining

 
Ishan Pandya
Ranch Hand
Posts: 225
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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..?
 
Stephan van Hulst
Bartender
Pie
Posts: 4838
34
Chrome Netbeans IDE Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 3742
16
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic