I wrote this piece of code, and it seems that the compiler allows you to access the uninitialized empty final field when accessed using the 'this' keyword:
public class TestClass { public final int value1; public int value2; TestClass(int value) { value2 = 2 + this.value1;
I tried to collect it on 1.5, 1.6 and 1.7 and got the same result in all three of them.
For me, this looks like a compiler error, because the compiler should throw an error in this case, but with the keyword 'this' it is not, and therefore creates an area of coding error, since it will go unnoticed by the programmer, since compilation or launch a time error will be triggered.
SEVERAL ITEMS WHY IT IS NOT A DUPLICATE
- all answers explain how it works and what JLS says, well, but should my real intention here be resolved in the first place?
- my question here is more from the point of view of the programmer, not the semantics of the language
java initialization final javac
sactiw Jun 16 '14 at 9:14 a.m. 2014-06-16 09:14
source share