Core Java

Unreachable Statement Java Error – How to resolve it

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

1. Introduction

Unreachable Statement Java

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. This code is also referred to 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 this error in detail about its occurrence in code with few examples.

Unreachable Statement Error occurs in the 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 the statement is placed right after the loop, this statement is never executed as the statements in the loop get 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, the 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. The compiler is able to evaluate the condition a>b to false. Hence, the body of the while loop is never executed and only the statement after the while loop gets executed. While we compile the code, the error is thrown at line 7 indicating the body of the loop is unreachable.

Note: if a and b are non-final, then the 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 Transfer Statements. They alter the flow of execution of a 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 the 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. The 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 the second example with a 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, the compiler throws an error at line 8, as the flow of execution comes out of for loop after break statement, and the print statement which is placed after break never get executed.

Finally, let us get into our final example with a continued 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, the compiler throws an error at line 10. At runtime as part of program execution, when the 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 learned about the Unreachable Statement Error in Java. We checked the reasons for this error through some examples and we have understood how to resolve it. You can learn more about it through Oracle’s page.

6. Download the Source Code

This was an example of the error Unreachable Statement in Java.

Download
You can download the full source code of this example here: Unreachable Statement Java Error – How to resolve it

Last updated on Jul. 23rd, 2021

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.
Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mr ushie john
Mr ushie john
2 years ago

Indeed this is great

Back to top button