Re: Incorrect "variable might not have been initialized"
Oliver Wong wrote:
....
With that in mind, if the compiler is "correctly" detecting a
definite assignment, even though the JLS says it shouldn't, we should
probably change the JLS to reflect that we can expect this extra bit of
usefulness, rather than changing the compiler to become "less useful"
and rigidly following the spec.
I think the JLS's philosophy is to try to avoid the following situation:
Program compiles clean on compiler A, with no deliberate warning or
error suppression.
Program needs to be compiled with compiler B. It spews out a ton of
error messages that have to be fixed before the programmer can get on
with whatever motivated the compiler change.
This could happen if compiler A were an optimizing compiler that did
elaborate data and control flow analysis, and decided that all uses of a
variable were dominated by assignments to that variable. Meanwhile,
compiler B is doing the minimum required by the spec.
The idea is to make "valid Java program" independent of the choice of
compiler. I'm not sure whether it is the right policy or not.
The spec is a little ambiguous in the assignment area. Assignment is not
specifically listed as one of the things that preserves constantness. On
the other hand, the result of the assignment, the value of the left hand
side variable, is, by definition, the result of converting the right
hand side. Casts to primitive types or String do preserve constantness.
Patricia