• 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

Superclass Reference

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test1910 {
float f;
Test1910(){
this(f);
f = 3;
}
Test1910(float f){
System.out.println(f);
}
public static void main(String args[]) {
Test1910 t = new Test1910();
}
}


When I compile the above program, the compiler says,

"cannot reference f before supertype constructor has been called this(f)."

but if pass this(4) it compiles and runs.

Could anyone explain the compiler error.

Thanks in advance,
Mithran
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is trying to say initialize the variable f before it is passed to another constructor,it is as similar as accessing a variable without initialisation.

cheers,
Santosh.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its not a problem of initialisation. f is an instance variable of the class.
It cannot be passed as argument to the constructor from within another constructor as the class is yet instantiated. Make f a static variable and the program will compile properly.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes..I think Abhijit is right. we can't access the instance variable before instanciate the object. As static variable is the class property we can access it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic