Dear Programmers, I have been trying to get the following program : 'Flowers' to run. It is meant to run off a group of subclasses to abstract class Flower. I have been getting the following output to the console, though:-
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\simon.BLUECHIP-KDOCDA>cd c:\lab_files\Lesson_3
I take it therefore that the switch statment is amiss, somehow? If so, then the error must lie with the two 'for' loops, which compiler okays now, but didn't previously. Hoping you might be able to give me a clue. Thank you for your attention. Yours Simon. code follows:-
The "break" is unreachable because the statement right before it exits the function. The compiler is trying to tell you that all of your "break"s are superfluous and should be removed.
The last error (missing return statement) occurs because it's at least theoretically possible to hit the end of the function without hitting one of those "return" statements; therefore there has to be a "return null" or something at the end of the function, even if it's never going to be used in practice. A better solution is to add a "default" case to the switch, which throws an IllegalArgumentException or something similar, so that the end of the function is definitely never reached.
Okay, for this case, it returns an object, and then??? When does the "break" get executed? Heck, can it be executed? What do you think the compiler is saying when it is complaining that this line is not reachable?
The issue is just that Java won't let you include "dead code" in a method -- i.e., statements that can never be executed are not legal. A "break" immediately after a return can never be executed, so it's not allowed.
In a "normal" switch -- i.e., a switch in which the various cases did not include a return statement -- the break statements would, indeed, be necessary (and, of course, legal.)
subject: Unreachable statements? (in program's switch statement)