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 What is this unreachable error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is this unreachable error" Watch "What is this unreachable error" New topic

What is this unreachable error

babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
Check it.
public class Cjgreen{
public static void main(String argv[]){
Cjgreen c = new Cjgreen();

public void jgreen(){
int iNum =1 ;
while(iNum >0){

for(int i = 0; i < 3; i ++){
continue toffer;

iNum --;
above code gives compilation error , says that System.out.println(i); is
than check below code :

public class Hello
public static void main(String argv[])
final int i=1;


above works fine with no errors , but here also System.out.println("test"); is unreachable .

can anybody explain the concept of unreachable correctly
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
The compiler handles 'if' statements as a 'special case' when determining wether or not a statement is reachable. It does not test the condition. This allows programmers to set up 'flags' that can be used in debugging code.

14.21 Unreachable Statements

As an example, the following statement results in a compile-time error:

while (false) { x=3; }

because the statement x=3; is not reachable; but the superficially similar case:

if (false) { x=3; }

does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.

The rationale for this differing treatment is to allow programmers to define "flag variables" such as:

static final boolean DEBUG = false;

and then write code such as:

if (DEBUG) { x=3; }

The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.
rajeshwar Rao
Ranch Hand

Joined: Mar 20, 2006
Posts: 31
If you observe this code for any value of i this will not reach system.out.println(i); i.e if you change for loop to some thing like
(for int=0;i<b;i++) where b can be 3 or 100 or 1000 or maximum value of integer but it will still not reach system.out.println(i); statement.

for(int i = 0; i < 3; i ++){
continue toffer;

But if you consider the below code:

final int i=1;

if you can change the value of i to some thing greater than six then there is a chance to reach system.out.println statement.

But in above case if you change the value of i to any thing even if for loop is modified to for(; it will not reach print statement. Hence it is giving unreachable error.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

if case is unreachable then y case 2 is not unreachable.
I agree. Here's the link:
subject: What is this unreachable error
Similar Threads
Indefinite loop
Flow control
Iterator expression in for statement.
break statement.