There is well known example from JLS
about incorrect forward reference
error:
class Test1 {
int i = j; // compile-time error:
// incorrect forward reference
int j = 1;
}
OK, I said, and apply well-known "hack" with keyword this
:
class Test1 {
int i = ++this.j;
{
// line#4: System.out.println("j = " + j); // compile-time error: illegal forward reference
System.out.println("j = " + this.j);
}
int j = this.j + 1;
{
System.out.println("j = " + j);
}
}
Output will be:
j = 1
j = 2
Who can explain, why I can not access to variable j
in line #4?
Is variable j
initialized in this line or not?
If it isn't, how I can get value 1
in this.j
and 2
on next step?
If if it is, why I can't get access via simple name
?