The compiler throws an error in accordance with JLS 8.4.7 , because it determines that the method can finish normally:
If the method returns a return value type, then a compile-time error occurs if the body of the method can complete normally.
To determine if a method can function normally, the compiler must determine if the for loop can complete normally, as defined in JLS 14.21
The base statement can complete normally if at least one of the following is true:
- A for statement is available, a condition expression exists, and the condition expression is not a constant expression with a value of true.
- There is a valid break statement that completes the for statement.
In the case of the third method, a condition expression exists, and it is not a constant expression, since i not final. Thus, the for statement can complete normally, and the method can also be the last.
QED
source share