• 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

Problem in Constructor Chaining

 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..?
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??

 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic