Assuming the following class
public class MyClass {
int a = b;
int b = 10;
}
The JLS 8.3.3. states in your case :
Use of instance variables whose declarations appear textually after the use is sometimes restricted
- The use is a simple name in either an instance variable initializer of C or an instance initializer of C
Now, using the member this
allows you to access an instance that is already declared with default values ( a = 0, b = 0
) but not yet fully initialized. This is visible if you check the result of:
public class MyClass {
int a = this.b;
int b = 10;
}
You would not get the expected value :
new MyClass().a //0
new MyClass().b //10
I can't explain why this is legal exactly since this would never give a correct value. We can find some explanation about why the restriction exist :
The restrictions above are designed to catch, at compile time, circular or otherwise malformed initializations.
But why allowing this
to works...
Knowing that during an initialization of an instance, the following actions occurs :
- Member declaration
- Block execution and Field initialization in order
- Constructor execution
Give some strange behavior:
public class MyClass {
{
b = 10;
}
int a = this.b;
int b = 5;
{
b = 15;
}
public static void main(String[] args) {
MyClass m = new MyClass();
System.out.println(m.a); //10
System.out.println(m.b); //15
}
}
I would limit the initialization in the constructor.