Hi I have below simple classes.
class A {
int i;
int y;
A(int i, int y)
{
}
and then when I try to do below
class B exte
Since A's constructor gets initialized first it has 0 value for both i and y and why cannot pass those values to super constructor.
The problem is that expressions for the arguments in super(i, y) (in B) are evaluated before calling the A constructor.
The execution sequence for new B() is as follows:
B and its dependents is triggered (if this hasn't happened already).B's constructor parameters are evaluated evaluated. (There aren't any, in this case.)B's type, and all fields (in B and its superclasses) are default initialized.super parameters for B are evaluated.super parameters for A are evaluated.Object() constructor body is executed.A.i and A.y would be initialized (if they had initializers).A(int,int) constructor body is executed. B's field initializers would be executed.B() constructor body is executed.B instance is returned. As you can see, step 4 refers to y which hasn't been initialized yet1. It won't be initialized until step 7.
Note: the above is simplified. Refer to JLS 15.9.4 for the full specification.
1 - The rules don't take account of the fact that there is no initializer in your example. But that's a good thing. 1) Taking account of that would make them more complicated and ... surprising. 2) What is the utility of allowing access to a variable if you know it has the default value? You could just use that value directly!