class A {
final int a;
A () {
this.a = 1; // No error
}
}
whereas the below code
class A {
final int a;
A () {
A.this.a = 1; // Error: java: cannot assign a value to final variable a
}
}
Using this.a to assign a value to a final field works fine. But using A.this.a to assign a value to the final field gives an error "java: cannot assign a value to final variable a". Not sure why?
I understand that there are no reason in this case to explicitly use A.this.a instead of this.a. Just wanted to understand the reason.
From this bug report, JVM architect John Rose mentioned that the definite assignment (DA) of blank final fields (e.g.
ain your code) are only tracked by the simple namea, andthis.a. Other more complex expressions likeA.this.aare not considered when determining whether the field is definitely assigned.He explains the reasoning further:
In short, this is an arbitrary choice. They kept the DA rules simple.