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)."
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.
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.
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