10

Why does initializing a string in an if statement seem different than in a switch statement? showed up in HNQ. A brief search turned up at least five duplicates:

At a bare minimum, one should be picked and the others closed as a duplicate of it.

But the fact I easily found five questions suggests that maybe some sort of canonical might also be a good idea. One could be cleaned up into a canonical, or a new canonical could be created.

The error can also arise in other cases, similar in that there's some flow of logic that allows for a variable to remain uninitialized, but that the author did not detect:

Decisions to make:

  • Should we also fold other questions about this error into the same question?
  • Should we create a separate canonical question rather than repurposing one of the ones I've linked? If not, which one should be kept?
2

2 Answers 2

2

I think the first candidate you listed is a reasonable dupe candidate. I've tidied it up and closed the recent question from HNQs.

The second candidate is a bit different ("I understand why compiler is warning me"). They're asking how to disable the check. Even though you can't do that, it's still a valid question. I've left it open.

The third is not the same but it wasn't worded very well. It's a good question in disguise. I've tidied it up.

The fourth is very low quality after all of those edits and it's just a dupe of the first one anyway. I closed it but personally I think it may as well be deleted.

The last asker is asking the same thing again. There's a misunderstanding of how assertions work thrown into the mix but I don't think that makes it fundamentally any different. I've closed it and added a second duplicate for the assertion stuff.

I don't think we need to do anything else. I browse the Java tag almost every day and I've never found this kind of question to be a problem.

4
  • 2
    Argh. Your question edit to the first question has invalidated rgettman's answer, which specifically addresses why the compiler can't tell that the case 2: case will always run; that explanation makes no sense now that you've made it so that the case 2: case isn't guaranteed to run. I'm going to manually rollback just that part of your question edit to make the answer fully make sense again. I'll let you decide how to proceed from there.
    – Mark Amery
    Jan 4, 2019 at 20:15
  • 1
    I agree that the fourth question, in its current state, is garbage, and would also be happy to see it deleted.
    – Mark Amery
    Jan 4, 2019 at 20:17
  • 1
    @MarkAmery Ah, that's my bad. Given that the compile-time constant is just a more specific version of the same problem (and is a usage of a switch statement that's useless in practice anyway), I think the change that I've just made is the best way to keep this question useful to a broad range of readers without invalidating the top answer.
    – Michael
    Jan 5, 2019 at 13:44
  • Just looked - that's a clever compromise. Thanks!
    – Mark Amery
    Jan 5, 2019 at 14:03
17

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.

  1. 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 switching 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

16
  • 3
    "Trying to squash every specific case into a single giant canonical will either result in that canonical being uselessly long and opaque": this is a prominent problem on Travel (in my opinion, at least). This is a good analysis.
    – phoog
    Jan 4, 2019 at 16:26
  • 1
    Good points about the questions being similar but having subtle distinctions that make them different and not duplicates of each other. Sometimes users may close questions as dupes not realizing the subtle differences that distinguish the questions, which means that those questions shouldn't be marked as a duplicate.
    – rgettman
    Jan 4, 2019 at 18:57
  • 2
    The problem is that all of them have the exact same fix: either assign a value or throw an exception in all code paths. Just because there are small, tiny, snowflake differences doesn't mean we need 20 different copies of the same answer lying around to be maintained. They're really all the same problem (the compiler can't guarantee that the variable has been initialized, literally like it says), just with the author failing to recognize the generality. That's why I suggested we need a canonical to explain the issue in a more situation agnostic way. -1
    – jpmc26
    Jan 4, 2019 at 19:43
  • And all the canonical needs to say is that only an assignment or an exception will satisfy the compiler and that this is absolutely required. That would categorically cover all of these cases, even the one asking how to "disable" it. These are all just different faces of the same polyhedron.
    – jpmc26
    Jan 4, 2019 at 19:48
  • @jpmc26 "And all the canonical needs to say is that only an assignment ... will satisfy the compiler" - but literally every single example you gave has an assignment, and in most cases the asker has correctly identified that it's an assignment that it's logically impossible not to reach before the variable is used. What is the point in closing the questions as a dupe of a canonical telling them to initialise their variables before using them when they're already doing that?
    – Mark Amery
    Jan 4, 2019 at 20:10
  • @MarkAmery If someone looking for this info doesn't know that the compiler does not always exhaust every possible value of a variable, then we can include that detail. Seriously. There's just not that much that needs explaining here. They're all just the same problem: there exists a code path that allows for an uninitialized variable. And if we want to make a practical point, we can note that more possible values for the input could be added later and that the compiler is protecting you from this. That may be especially important in light of the fact that Java is not statically linked.
    – jpmc26
    Jan 4, 2019 at 20:16
  • @jpmc26 Okay, but 1) that hypothetical expanded canonical is still not going to cover people who are asking questions about why Java can't recognize that a variable is initialised in some particular circumstances, which two of your examples explicitly are, and 2) even leaving those spec questions aside, it's still worth distinguishing (a) someone who just straight up isn't initialising their variable before using it and doesn't understand what the error is telling them, (b) someone who wrongly thinks they are initialising it but aren't due to a logic error, and ... [1/2]
    – Mark Amery
    Jan 4, 2019 at 20:39
  • [2/2] ... (c) someone who really is guaranteeing initialisation before use and wants to know how to shut up the erroneous-looking compiler error. Person (a), (b), and (c) need fundamentally different answers from each other. Why can't they then just... be three different questions? How does it benefit the reader for those three scenarios to be addressed in turn in one mega-answer that covers all of them, instead of having one Q&A for each?
    – Mark Amery
    Jan 4, 2019 at 20:42
  • "why Java can't recognize that a variable is initialised in some particular circumstances" This is absurd. There's no reason to expect that it can. Why can't the computer write my code based on me speaking natural language to it? Because no one has figured out an efficient way to make that work. At some point, why questions just become useless: youtu.be/Dp4dpeJVDxs. It is enough to say that the compiler simply does not exhaust all values of a variable (at least usually), and this covers the other cases you want to separate out as well.
    – jpmc26
    Jan 4, 2019 at 20:43
  • @jpmc26 Well, that's a defensible argument, although I don't agree with it, and find the specific details of Java's flow analysis discussed in the questions you've linked to valuable and interesting, and think it is a misguided venture to try and stamp out the questions that led to those details being posted. But even if I agreed with it entirely, it still wouldn't make those questions duplicates of a question about how to fix the error.
    – Mark Amery
    Jan 4, 2019 at 20:45
  • As for why they shouldn't be separate, because they all have the same answer: make all code paths either assign a value or throw an exception. That is the only answer. Duplicating that answer all over the place is useless. Duplicate questions are fine (if closed). Duplicate answers are detrimental and create a maintenance burden and also diffuse information that would be more valuable if it were all centralized.
    – jpmc26
    Jan 4, 2019 at 20:46
  • @jpmc26 That's not an answer in any practical sense, so much as a refusal to answer. It contains no information for someone who thinks they've already done that and has explicitly stated that in their question, like literally every single example you've given. You might as well say that all debugging questions are really duplicates of a single question whose answer is "fix the bug". But we started this argument on that point; I think we're now going in circles.
    – Mark Amery
    Jan 4, 2019 at 20:48
  • It is an answer. It's saying, "Your assumption that you did so is wrong." And as I've stated numerous times, we can include info like, "The compiler does not exhaust all the variable values." We don't need a separate question/answer to exhaust every possible way in which their assumptions might be wrong. Allowing that would mean that SO is an individual help service, not a high quality information repository, and would justify eliminating closure altogether. We expect users to have the fundamental programming skill of slightly generalizing information to their particular situation.
    – jpmc26
    Jan 4, 2019 at 20:57
  • @jpmc26 But I'm not advocating for allowing a limitless number of debugging questions on this topic. So far - leaving aside the fate of the non-debugging, spec-based questions - I've advocated for a grand total of three, covering three entirely distinct debugging scenarios whose solutions have no overlap with each other. That is a long way from making SO an "individual help service".
    – Mark Amery
    Jan 4, 2019 at 21:06
  • "No overlap." Right. Three scenarios that can be summed up as, "You have a code path that neither initializes the variable nor throws an exception," have nothing in common. Right. I guess this comment has no overlap with my previous ones, by that logic. Fundamentally, your argument allows for infinitely many more debugging scenarios, whether or not you're advocating for them explicitly.
    – jpmc26
    Jan 4, 2019 at 21:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .