Use of uninitialized final field - with/without 'this.' qualifier
Solution 1:
After a bunch of spec-reading and thought, I've concluded that:
In a Java 5 or Java 6 compiler, this is correct behavior. Chapter 16 "Definite Assignment of The Java Language Specification, Third Edition says:
Each local variable (§14.4) and every blank
final
(§4.12.4) field (§8.3.1.2) must have a definitely assigned value when any access of its value occurs. An access to its value consists of the simple name of the variable occurring anywhere in an expression except as the left-hand operand of the simple assignment operator=
.
(emphasis mine). So in the expression 2 * this.x
, the this.x
part is not considered an "access of [x
's] value" (and therefore is not subject to the rules of definite assignment), because this.x
is not the simple name of the instance variable x
. (N.B. the rule for when definite assignment occurs, in the paragraph after the above-quoted text, does allow something like this.x = 3
, and considers x
to be definitely assigned thereafter; it's only the rule for accesses that doesn't count this.x
.) Note that the value of this.x
in this case will be zero, per §17.5.2.
In a Java 7 compiler, this is a compiler bug, but an understandable one. Chapter 16 "Definite Assignment" of the Java Language Specification, Java 7 SE Edition says:
Each local variable (§14.4) and every blank
final
field (§4.12.4, §8.3.1.2) must have a definitely assigned value when any access of its value occurs.An access to its value consists of the simple name of the variable (or, for a field, the simple name of the field qualified by
this
) occurring anywhere in an expression except as the left-hand operand of the simple assignment operator=
(§15.26.1).
(emphasis mine). So in the expression 2 * this.x
, the this.x
part should be considered an "access to [x
's] value", and should give a compile error.
But you didn't ask whether the first one should compile, you asked why it does compile (in some compilers). This is necessarily speculative, but I'll make two guesses:
- Most Java 7 compilers were written by modifying Java 6 compilers. Some compiler-writers may not have noticed this change. Furthermore, many Java-7 compilers and IDEs still support Java 6, and some compiler-writers may not have felt motivated to specifically reject something in Java-7 mode that they accept in Java-6 mode.
- The new Java 7 behavior is strangely inconsistent. Something like
(false ? null : this).x
is still allowed, and for that matter, even(this).x
is still allowed; it's only the specific token-sequencethis
plus.
plus the field-name that's affected by this change. Granted, such an inconsistency already existed on the left-hand side of an assignment statement (we can writethis.x = 3
, but not(this).x = 3
), but that's more readily understandable: it's acceptingthis.x = 3
as a special permitted case of the otherwise forbidden constructionobj.x = 3
. It makes sense to allow that. But I don't think it makes sense to reject2 * this.x
as a special forbidden case of the otherwise permitted construction2 * obj.x
, given that (1) this special forbidden case is easily worked around by adding parentheses, that (2) this special forbidden case was allowed in previous versions of the language, and that (3) we still need the special rule wherebyfinal
fields have their default values (e.g.0
for anint
) until they're initialized, both because of cases like(this).x
, and because of cases likethis.foo()
wherefoo()
is a method that accessesx
. So some compiler-writers may not have felt motivated to make this inconsistent change.
Either of these would be surprising — I assume that compiler-writers had detailed information about every single change to the spec, and in my experience Java compilers are usually pretty good about sticking to the spec exactly (unlike some languages, where every compiler has its own dialect) — but, well, something happened, and the above are my only two guesses.
Solution 2:
When you use this
in the constructor, compiler is seeing x
as a member attribute of this
object (default initialized). Since x
is int
, it's default initialized with 0
. This makes compiler happy and its working fine at run time too.
When you don't use this
, then compiler is using x
declaration directly in the lexical analysis and hence it complains about it's initialization (compile time phenomenon).
So It's definition of this
, which makes compiler to analyze x
as a member variable of an object versus direct attribute during the lexical analysis in the compilation and resulting into different compilation behavior.
When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed.