• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is this unreachable error

 
babu sharath
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check it.
public class Cjgreen{
public static void main(String argv[]){
Cjgreen c = new Cjgreen();
c.jgreen();

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

toffer:
for(int i = 0; i < 3; i ++){
continue toffer;
System.out.println(i);
}
}

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

public class Hello
{
public static void main(String argv[])
{
final int i=1;
if(i>5)
System.out.println("test");
}

}

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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
System.out.println(i);
}


But if you consider the below code:

final int i=1;
if(i>5)
System.out.println("test");
}

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
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if case is unreachable then y case 2 is not unreachable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic