You need to initialize it in advance or add a default clause:
switch (1) { case 1: b = true; default: b = false; }
It is simply not possible for the JVM to analyze all possible cases, not even for a literal. At least, as @assylias points out, the language specification does not require it to be.
Therefore, from the point of view of code analysis, it must process the literal as well as process the variable, and cannot know that the selected path is always selected, even here, where we can easily see that the first case will always correspond.
So, he should see that the variable b initialized no matter what the value is, and therefore requires a default clause.
source share