The moose likes Beginning Java and the fly likes Unreachable statements? (in  program's switch statement) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unreachable statements? (in  program Watch "Unreachable statements? (in  program New topic
Author

Unreachable statements? (in program's switch statement)

Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
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

C:\lab_files\Lesson_3>set PATH=c:\Sun\AppServer\jdk\bin;%PATH%


C:\lab_files\Lesson_3>javac Flowers.java
Flowers.java:19: unreachable statement
break;
^
Flowers.java:22: unreachable statement
break;
^
Flowers.java:25: unreachable statement
break;
^
Flowers.java:28: unreachable statement
break;
^
Flowers.java:31: unreachable statement
break;
^
Flowers.java:35: missing return statement
}
^
6 errors

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:-









Simon
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Your cases all look like



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.


[Jess in Action][AskingGoodQuestions]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40



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?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Got beaten to the answer by one minute...
Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
Dear Henry Wong & Ernest Friedman Hill,
I got rid of the break statements as you suggested, see code excerpt below:-

getting:-

or:-

Which I am pleased about, all the same I was wondering - only one of the switch statements is required to be

called at a time - so why aren't the 'break' statements necessary? I thought they were integral to the switch

statement - or else all the other statements would execute- okay so they don't in this case - so why didn't

they ?- sorry to be a pain- but you must've experienced similar such conundrums ie: Why isn't it not working?
Thank you for your help, all the same, hoping to hear,
Yours
Simon.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Simon,

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.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unreachable statements? (in program's switch statement)