The line i2=8 is not initialisation. It is plain old assignment. Initialisation is giving a variable an initial value when you declare it. For local variables you must intialise them yourself. Whereas for instance variables if you do not initialise them they are intialised by the compiler with a default value.

So why does the compiler complain? What happens when the if statement is false? What value does i2 have then?

The compiler doesn't do any interpretation on (the source of) your problem. It just checks that i2 is only initialized if an if-condition succeeds; if it doesn'out succeed, that variable isn't initialized; you, on the other hand, do perform some form of interpretation and you can conclude that that variable must always be initialized because that if-condition always succeeds. If you make that other variable final, the compiler 'knows' the value of that variable and it can deduce that the if-condition succeeds and your variable i2 is always initialized (so it doesn't complain).

kind regards,

Jos

I have the stamina of a seal; I lie on the beach instead of running on it.

For the first 2 versions we can see that the if statement will not be entered. With the 3rd version we have no idea if it will enter or not unless we have the source of the method as well. Now what the compiler needs to do is have consistent behavior across all 3 versions as well as yours. I certainly would not trust a compiler if it behaves differently just because you want it to.