File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion" Watch "Assertion" New topic


srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225

compilations fails at line 1

unreachable statement. please explain what does it mean

SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274

Remember, that using assertions depends on wheter you enable them or not when you start the application. For example, when you use your main2 class (with line 1 //commented out it compiles nicely) like

When you start this user class, with
>java UserOfm2

then the assertions are by default disabled, and you just get the sentence from the main-method.

but when you use
>java -ea UserOfm2
an assertion error will be called from the foo() method and the program stops after the first assertion of false.

Because of the fact that the compiler does not know if later the program will be called with assertions enabled or disabled, two assertions of false one after the other cannot be a compile time error due to unreacheable code.

The situation below in your original main2 class is different:

This produces a compile time error due to "unreacheable code". But here, the fact that line 1 is also an assertion is not so important. Any code at the place of line 1 would cause this compile time error.
Because in

true is a compile time constant, a boolean literal, and the compiler knows for sure, that this loop (without any break in it) can never been left.
Therefore anything that is "behind" this loop is unreachable code that produces a compile time error that forces the programmer to fix this bug.

And again the code inside the loop does not necessarily cause the loop to be left because again you could start your application with the assertions disabled.

[ October 13, 2007: Message edited by: Burkhard Hassel ]

all events occur in real time
I agree. Here's the link:
subject: Assertion
It's not a secret anymore!