The
Java Language Specification states:
JLS wrote:Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.
You think (and know) that the statement will never be reached. But for the compiler it is reachable! The reason is, as stated by JLS, that the compiler does not examine what is written inside
if as a condition.
You could even write:
and for the compiler the statement would be reachable.
It is not hard. You are just overthinking it.
Of course this is also not super easy. The JLS states that while, do and for loops are treated differently.
This won't compile:
And this will compile: