Home » Core Java » Unreachable Statement Java Error – How to resolve it

About Shaik Ashish

Shaik Ashish
He completed his Bachelors Degree in Computer Applications. He is a freelancer, writer, Microsoft Certified in Python and Java. He enjoys Writing, Teaching, Coding in Java and Python, Learning New Technologies and Music.

Unreachable Statement Java Error – How to resolve it

In this post, we shall look into Unreachable Statement Java Error, when does this occur and its resolution.

1. Introduction

Unreachable Statement is an error raised as part of compilation when java compiler detects code which is never executed as part of execution of program. Such code is obsolete, unnecessary and therefore it should be avoided by the developer. This code is also referred as Dead Code, which means this code is of no use.

2. Explanation

Now that we have seen what actually is Unreachable Statement Error is about, let us discuss about this error in detail about its occurrence in code with few examples.

Unreachable Statement Error occurs in following cases

  1. Infinite Loop before a line of code or statements
  2. Statements after return, break or continue

2.1 Infinite Loop before a line of code or statements

In this scenario, a line of code is placed after an infinite loop. As statement is placed right after the loop, this statement is never executed as the statements in loop gets executed repeatedly and the flow of execution is never terminated from the loop. Consider the following code,

Example 1
public class JavaCodeGeeks
{
	public static void main(String[] args) {
		while(true)
		{
			System.out.println("Hi");
		}
		System.out.println("Hello");
	}
}
Output
JavaCodeGeeks.java:8: error: unreachable statement
                System.out.println("Hello");

In this example, compiler raises unreachable statement error at line 8 when we compile the program using javac command. As there is an infinite while loop before the print statement of “hello”, this statement never gets executed at runtime. If a statement is never executed, compiler detects it and raises an error so as to avoid such statements.

Now, let us see one more example which involves final variables in the code.

Example 2
public class JavaCodeGeeks
{
	public static void main(String[] args) {
		final int a=10;
		final int b=20;
		while(a>b)
		{
			System.out.println("Inside while block");
		}
		System.out.println("Outside while block");
	}
}
Output
JavaCodeGeeks.java:7: error: unreachable statement
                {
                ^
1 error

In the above example, variables a and b are final. Final variables and its values are recognized by the compiler during compilation phase. Compiler is able to evaluate the condition a>b to false. Hence, body of the while loop is never executed and only the statement after while loop gets executed. While we compile the code, error is thrown at line 7 indicating body of the loop is unreachable.

Note: if a and b are non-final, then compiler will not raise any error as non-final variables are recognized only at runtime.

2.2 Statements after return, break or continue

In Java language, keywords like return, break, continue are called as Transfer Statements. They alter the flow of execution of program and if any statement is placed right after it, there is a chance of code being unreachable. Let us see this with an example, with return statement.

Example 3
public class JavaCodeGeeks
{
	public static void main(String[] args) {
		System.out.println(foo(40));

	}
	public static int foo(int a)
	{
		return 10;
		System.out.println("Inside foo()");
	}
}
Output
JavaCodeGeeks.java:10: error: unreachable statement
                System.out.println("Inside foo()");
                ^
JavaCodeGeeks.java:11: error: missing return statement
        }
        ^
2 errors

In this example, when foo() method is called, it returns value 10 to the main method. Flow of execution inside foo() method is ended after returning the value. When we compile this code, error is thrown at line 10 as print statement written after return becomes unreachable.

Let us look into second example with break statement.

Example 4
public class JavaCodeGeeks
{
	public static void main(String[] args) {
	  for(int i=1;i<5;i++)
	  {
	  	System.out.println(i);
	  	break;
	  	System.out.println("After break");
	  }
	}
} 
Output
JavaCodeGeeks.java:8: error: unreachable statement
                System.out.println("After break");
                ^
1 error 

When the above code is compiled, compiler throws an error at line 8, as flow of execution comes out of for loop after break statement and print statement which is placed after break never gets executed.

Finally, let us get into our final example with continue statement.

Example 5
public class JavaCodeGeeks
{
	public static void main(String[] args) {
		for(int i=0;i<8;i++)
		{
			System.out.println(i);
			if(i==5)
			{
				continue;
				System.out.println("After continue");
			}
		}
	}
} 
Output
JavaCodeGeeks.java:10: error: unreachable statement
                                System.out.println("After continue");

When the above code is compiled, compiler throws an error at line 10. At runtime as part of program execution, when the i value increments to 5, if block is executed. Once the flow of execution encounters continue statement in if block, immediately the flow of execution reaches the start of for loop thereby making the print statement after continue to be unreachable.

3. Resolution

Keep these points in your mind so that you avoid this error while compilation of your code.

  1. Before compiling, examine the flow of execution of your code to ensure that every statement in your code is reachable.
  2. Avoid using statements which are not at all required or related to your programming logic or algorithm implementation.
  3. Avoid statements immediately after return, break , continue.
  4. Do not place code after infinite loops.

4. Unreachable Statement Java Error – Summary

In this post, we have learnt about Unreachable Statement Error, reasons for this error with few examples and finally we have understood resolution of this error.

5. Download the Source Code

This was an example of Unreachable Statement Error in Java.

Download
You can download the full source code of this example here: Unreachable Statement Java Error – How to resolve it
(+2 rating, 2 votes)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of