posted 23 years ago
This is an unexpected result, and I'm not sure why it is happening, but you can get the expected result if you initialize and assign a value to j before you run the s() method on i.
class A
{
int j=10;
int i = s();
int s()
{
System.out.println("j is " + j);
return j;
}
public static void main(String str[])
{
A a=new A();
System.out.println("a.j is " + a.j);
System.out.println("a.i is " + a.i);
}
}
Consequently, I think that the instance variables are initialized first (ints to 0s) and then the values are assigned in order of their declaration. Since s() is called to return j before j is assigned a value of 10, it is returning 0. This is just a theory, though - maybe someone has a more definitive answer?