A compilation error occurs. This is due to the assignment statement in the Subclass class. Is this due to the fact that instance variables and methods are not available until the superclass constructor completes? I just want to make sure that my understanding is correct.
Current Status:<br /> <br />SCJP 1.4<br />SCJD (in progress)
But u cant initailise that just anywhere, it should be done within a block u can only declare items outside. Like u can have also this:
public class SuperClass { protected int i = 10; } class Subclass extends Superclass { { i = 15; } public void method() { System.out.println("i is " + i); } }
So back to my question. Is the compilation failure due to the fact that instance variables are not available until the super constructor runs? I found this rule in the K & B book, but not sure if this is exactly what the rule meant.
Current Status:<br /> <br />SCJP 1.4<br />SCJD (in progress)
Originally posted by Liang Anmian: So back to my question. Is the compilation failure due to the fact that instance variables are not available until the super constructor runs? I found this rule in the K & B book, but not sure if this is exactly what the rule meant.
As mentioned above, your code is syntatically not correctJava.
Yup it's wrong. In my example, the instance variable defined in the superclass is protected. Since it's protected, the subclass has access to the variable.
Current Status:<br /> <br />SCJP 1.4<br />SCJD (in progress)