I've pretty much never touched Java and am open to being persuaded that I'm misguided, but for what it's worth, I'd advise you to tread carefully here. There are a lot of distinct scenarios covered by those questions, with subtly different answers, together covering different ground.
Why do I get a "variable might not have been initialized" compiler error in my switch block? covers the compiler not being clever enough to statically detect which branch will run when switch
ing on the value of a local variable immediately after assigning a literal to it. The answer explains why this is so with specification quotes about the concept of "definite assignment", and shows how to fix it by adding a default
case to the switch
.
Java complains about final field not initialized in default case of a switch is from someone who's already added a default case, but is being caught out by their wrong assumption that an assert false
will be treated by the compiler as a guaranteed exit point from the function when really it isn't. The fix involves converting the assert
statement to a throw
statement. There is no overlap at all between this and #1, even though they're clearly related.
How to switch off "java: variable might not have been initialized" is about the compiler not being clever enough to detect that control flow won't continue past a System.exit()
call. The accepted answer doesn't look like the neatest possible solution, to me, and the highest upvoted answer is some high-rep user completely failing to engage with the substance of the question and instead patronising the asker and generically telling them to "fix" their code.
Why, when I have cases for every enum constant in a switch statement, must I still provide a default? is about the compiler not being clever enough to detect that a switch
on the value of an enum variable must go into the case
corresponding to one of the defined enum values. The solution is, as with 2, to add a logically-impossible default case.
Variable not Initialized - Although I am? is (I think - unless my poor Java skills have led me to misunderstand any of the prior questions) the only case in the entire batch where the compiler has really detected a logically possible scenario in which a variable is uninitialised before use. The answer is basically to point out this logic error.
None of these look like the same question to me. I'm very much opposed to your "bare minimum" suggestion of picking one as a dupe target for all the others; these are all about different scenarios, and they mostly have different solutions.
Note also that questions 2-4 all currently lack any answer citing the spec and explaining why the compiler wasn't clever enough to detect the logical impossibility of the flow path it was complaining about. Such answers would add value to those questions, and each question would require a significantly different answer since different bits of spec are relevant to each question.
There might be room for a canonical question that all of these could be closed as a dupe of. I'm honestly not yet convinced by that either. To explain why the compiler is being tripped up by all imaginable scenarios in which it might be tripped up, wouldn't you need to basically reproduce all the information in https://docs.oracle.com/javase/specs/jls/se8/html/jls-16.html? I haven't read it, but it looks long.
I'm inclined to think the right answer is to keep these separate, tidy them up with edits, and enhance them with better answers (including but not limited to ones that cite the bits of the definite assignment spec that are relevant to each specific case). Trying to squash every specific case into a single giant canonical will either result in that canonical being uselessly long and opaque, or in lots of specific nuances being left uncovered, even though those specific nuances may frequently be exactly what some particular Google searcher cares about. It's okay for questions that are closely related but still distinct to just carry on coexisting.
System.exit
.