Community
Participate
Working Groups
build 20020312 The following method might meet the java spec but the error message seems strange at first and only makes sense later. Object getObject() { Object myObject = new Object();; return myObject;; } No errors are shown in line 1 due to ;; but on line 2 it displays a "Unreacheable code" message. In this simple method it is easy to find the duplicate semi-colon but in a more complex structure the user could "get blind" and not see it. If possible, the message should be more clear and indicate the real problem: (duplicate semi-colon after return statement).
';' represents an empty statement. So this statement is unreachable after a return. So if you write: Object myObject = new Object();; The first semi-colon closes the local declaration statement and the second semi-colon is simply an empty statement. You can perfectly write this. When you write: return myObject();; The first semi-colon closes the return statement and the second one is an empty statement. The JLS specifies that all statements must be reachable (http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236365). In your example, the first empty statement is reachable, because a field declaration doesn't exit the method. However the second empty statement is unreachable, because the flow of the execution will never reach it. It is after a return statement and this statement exits the method. So as the JLS specified, the compiler returns an error saying that this empty statement is unreachable. If you double-click on the error in the task view, you will have the second semi- colon highlighted, which means this is the unreachable code. Some compilers were ignoring this error. Now we correctly reject such a code. Changing the error message for something like "duplicate semi-colon after return statement" would be wrong. This message is used for all unreachable code. You could write: return myObject;System.out.println(); In this case we flag in the same way the whole statement System.out.println(); has been unreachable. The difference is that this is not an empty statement. So we have a generic message to report unreachable code and as long as we highlight the whole statement (reduced to a single semi-colon in the case of an empty statement) I don't see anything wrong with that. Ok to close?
Sure.
Empty statements are unfortunately true statements, and have to be reported by the compiler when unreachable. Closing
*** Bug 72467 has been marked as a duplicate of this bug. ***