In the following code:
private static void example() { String inputString = "test"; switch (inputString) { case "test": String outputString = "The string said test"; case "not test": String outputString = "The string did not say test"; } }
Eclipse highlights the line after Case "not test" with a Duplicate local variable outputString . However, since they are in separate branches of the switch , they do not actually conflict, since only one can ever act at a time.
Obviously, in this case, I could work around the problem by moving the outputString outside of the switch . However, in more complex programs, the solution may not be so simple.
Is this just an error caused by Eclipse to prevent a bad programming technique or an actual Java error?
I am very new to Java, so I apologize if this is a newbie question. I had Google for "Duplicate a local Java variable", but so far all that has appeared is people who ask for help in fixing the problem (the usual solution is to rename one of the variables), and not discuss the problem itself.
source share