Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in Constructor Chaining

 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • 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: 5790
61
  • Likes 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: 226
Java
  • 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
Posts: 3742
16
  • Likes 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: 4567
8
  • Likes 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: 226
Java
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic