Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unreachable statement compile error in Java [duplicate]

class For1
{
  public static void main(String args[])
  {
    int a = 0;
    for(;;)
    {
      break;
      System.out.println(a); //Line 1
      ++a;//Line 2
    }
  }
}

I know that Line 1/Line 2 will never be executed. But still I don't understand why a compile time error is thrown. I am getting "unreachable statement" compile error.

Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?

like image 311
UnderDog Avatar asked Aug 19 '13 07:08

UnderDog


People also ask

How do I fix unreachable statement error in Java?

1(a) is compiled, line 12 raises an unreachable statement error because the break statement exits the for loop and the successive statement cannot be executed. To address this issue, the control flow needs to be restructured and the unreachable statement removed, or moved outside the enclosing block, as shown in Fig.

Is unreachable statement a compile error?

An unreachable Statement is an error raised as part of compilation when the Java compiler detects code that is never executed as part of the execution of the program. Such code is obsolete, unnecessary and therefore it should be avoided by the developer.

Why is Java statement unreachable?

The Unreachable statements refers to statements that won't get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the following reasons: Have a return statement before them. Have an infinite loop before them.


3 Answers

Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?

It means the compiler checks that every statement is reachable.

From section 14.21 of the JLS:

It is a compile-time error if a statement cannot be executed because it is unreachable.

This section is devoted to a precise explanation of the word "reachable." The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer, or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements.

The section then documents how reachability is defined.

In particular, the relevant points in your case are:

Every other statement S in a non-empty block that is not a switch block is reachable iff the statement preceding S can complete normally.

A break, continue, return, or throw statement cannot complete normally.

So your "line 1" statement is preceded by a statement (break;) which cannot complete normally, and therefore it's unreachable.

like image 146
Jon Skeet Avatar answered Oct 23 '22 00:10

Jon Skeet


The compiler is also able to make that conclusion, and assumes you are making a mistake. And yes, the Java compiler does a pretty good amount of "Data-Flow Analysis". The most common related message is the one about variables not initialized. The second most frequent is, I believe, precisely this one, about code not reachable.

like image 23
Mario Rossi Avatar answered Oct 22 '22 23:10

Mario Rossi


Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?

Yes compiler compiles the whole body of code and make byte code according to your code, it smarter enough to detects unreachable code also dead code. Immediate break in the for-loop makes unreachable other statements.

for(;;){
   break;
   ... // unreachable statement
}


int i=1;
if(i==1)
  ...
else
  ... // dead code
like image 4
Subhrajyoti Majumder Avatar answered Oct 23 '22 01:10

Subhrajyoti Majumder