• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic