Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
cmbhatt
Intance variables get their default values ,after completion of executing the super class constructor
until the super class constructor completes you can't use the subclass member variables because they are not initialized.
Notice that the error message refers to the supertype constructor being called.
Remember, when an object
is constructed using new (as opposed to being deserialized), the following things happen (in this order):
1. All instance variables are assigned default values.
2. The constructor is invoked, which immediately invokes the superclass
constructor (or another overloaded constructor, until one of the overloaded
constructors invokes the superclass constructor).
super(xx,yy);//1) this gives compiler error
An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.
The constructor of class Object has not been called before f is referenced in the explicit call to this(f). It is not done until the constructer Test(float) is executed. So according to the above specification an error is generated by the compiler.
cmbhatt
Hey Sasha!
This all is very confusing....
Once the super class constructor has completed execution,then all the instances variables of the subclasses will get their default values.
Once the super class constructor has completed execution,then all the instances variables of the subclasses will get their default values.
cmbhatt
"Whydoes the compiler complains for the above code?" Obviously the compiler complains, obviously it is forbidden, but why?
cmbhatt
Originally posted by Sasha Ruehmkorf:
i get the error message:
"Cannot refer to an instance field f while explicitly invoking a constructor". Super Constructor is not mentioned there.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |