aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Says cannot reference f before supertype constructor has been called Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Says cannot reference f before supertype constructor has been called" Watch "Says cannot reference f before supertype constructor has been called" New topic
Author

Says cannot reference f before supertype constructor has been called

Prashanth Muthyala
Greenhorn

Joined: Jan 20, 2005
Posts: 5
public class Test19 {
float f;
Test19(){
this(f); //I did not get this line....
f = 3;
}
Test19(float f){
System.out.println(f);
}
public static void main(String args[]) {
Test19 t = new Test19();
}
}
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Basically, a class's instance variables can't be accessed until all the parent constructors have done their thing .. so obviously, you can't send an instance variable as an argument to a parent constructor since it isn't really there yet.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Prashant,
Its not giving the error as u said, its saying "Cannot refer to an instance field f while explicitly invoking a constructor"
An instance field belongs to an instance. So, i guess before creating an instance u are trying to use the instance field, thats why the error props up.

But u can use a static field as that belongs to a class
like this is true:
public class Test19 {
static float f;
Test19(){
this(f); //No error here
f = 3;
}
Test19(float f){
System.out.println(f);
}
public static void main(String args[]) {
Test19 t = new Test19();
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Says cannot reference f before supertype constructor has been called