An unreachable code statement is a common problem among Java beginners. A lot of novice developers confuse the error with “dead code” - another Java-related phenomenon. Although the two are similar by manifestation, there are some differences which we will cover in this post.
Other than that, you will find out what the most common reasons for your compiler returning an unreachable code statement are and discover some easy fixes to get your code up and running again.
![Unreachable Statement Code Error in Java - 2]()
What Is Unreachable Code?
By definition, an unreachable statement is the one that will not be executed by a compiler when you run ready-to-deploy code. An unreachable code return statement is typically a sign of a logical error within the program. Although there are several reasons why you end up with such a statement, in all cases, unreachable code is redundant, clutters your program, and should be avoided at all costs.Unreachable Code vs Dead Code
In the development community, the concepts of “unreachable” and “dead” code are practically synonymous. However, if you are nitpicky when reading the documentation you might see that development guidelines often mention the two separately. Is there any difference between dead and unreachable code? Basically, the distinction between the two errors is in the way the compiler reacts to them. If the code you enter in the compiler is unreachable, you’ll get a compile runtime error in Java notification. If your statement is “dead code”, there will be no runtime errors - a developer will get the following system warning:
class DeadCode {
void deadcode_Method(boolean b) {
System.out.println("Reachable statement");
if(true) {
return;
}
System.out.println("Unreachable statement"); // dead code
}
}
Since there’s no direct compiler error in the case of dead code, it’s harder to detect. However, if you keep careful track of System.out.printIn returns, catching dead code shouldn’t cause you trouble.Why You Get Unreachable Code Statements
The good news is, it’s easy to trace the cause of unreachable code issues. There are three main reasons why your compiler keeps returning errors:- Transfer statements. If you break your code with a return statement, nothing after “return = true” will be executed.
- Infinite loop - no code you wrote after the infinite loop will be executed either since the system will keep reiterating the loop action. Thus, when converting your code in byte code, the compiler will send an unreachable code error.
Return statements
A return statement is a part of the Transfer keyword group, meaning that it terminates your method. It is helpful for separating functions and helps keep your code readable and clean. However, since you can’t add new statements to the function after return = true, trying to continue the function after the keyword will give you an “Unreachable code” compiler error. Let’s take a look at an example of “return = true” mishandling and the way a compiler reacts to it.
class GFG {
public static void main(String args[])
{
System.out.println("My code will run");
return;
// ironically, this code will never run
// here’s an unreachable code message a developer gets.
System.out.println("My code will run");
}
}
Here’s how a return statement misuse will be displayed in the compiler:
prog.java:11: error: unreachable statement
System.out.println(“My code will run”);
^
1 error
To reinforce what you learned, we suggest you watch a video lesson from our Java CourseBreak statements
Break statements are another type of keywords you need to be careful with when writing Java functions. By the definition, the break keyword is used to terminate a loop. In the example below, as a developer exits the loop, he will no longer be able to execute the statement on line 8 - thus, the compiler will show an unreachable statement error. Here is the sample code that will result in an unreachable code statement:
public class JavaCodeGeeks
{
public static void main(String[] args) {
for(int i=1;i<5;i++)
{
System.out.println(i);
break;
System.out.println("Code after break");
}
}
}
Looking at the error from a compiler point-of-view, you’ll get the following error statement.
JavaCodeGeeks.java:8: error: unreachable statement
System.out.println("After break");
^
1 error
Continue statements
Continue is a loop control keyword used to reiterate actions. Whenever you want the execution of a loop to start from scratch on its own, add continue to your code. The statement is useful to help developers choose which statements of the loop they want to reiterate and the ones they’d not put in the iteration. Although continue is a straightforward keyword to use, not having full understanding of how it works leads developers to the “unreachable code” trap. Since, after encountering a continue, a system will reiterate the loop, the keyword will not be able to reach the statements that follow it. Say, you have the following code:
public class JavaIsFun
{
public static void main(String[] args) {
for(int i=0;i<8;i++)
{
System.out.println(i);
if(i==5)
{
continue;
System.out.println("Coding after continue");
}
}
}
}
The system will not execute your “Coding after continue” statement - the compiler will let you know about it right away.
JavaIsFun.java:10: error: unreachable statement
System.out.println("Coding after continue");
Infinite loops
A scenario that’s similar to the examples of “break” and “continue” keyword use cases is that of an infinite loop. When designing an infinite loop, a developer should remember that no statement after it will ever run. Thus, if you don’t break the loop, all the code written after will be unreachable. Here’s an interesting example of infinite loop mishandling to check out:
public class JavaCodeGym
{
public static void main(String[] args) {
while(true)
{
System.out.println("Hey there");
}
System.out.println("Nice to see you");
}
}
Can you guess where the error is hiding? The compiler will point at it right away once you run your code:
//unreachable code statement compiler error
JavaCodeGym.java:10: error: unreachable statement
System.out.println("Nice to see you");
‘
Since there’s an infinite loop prior to "Nice to see you", the statement will never execute and keep returning an unreachable code error.
GO TO FULL VERSION