Javatpoint Logo
Javatpoint Logo

Java missing return statement

The missing return statement is one of the most occurred errors in the Java program. The beginners usually face the missing return statement error. It is a compile-time error, it means that the error is raised when we compile program. The name of the error states the root cause of the error is that the return statement is missing in the program.

The Java missing return statement error mainly occurs due to human mistakes. It occurs when we use the return type before the method name and doesn't use the return statement. In Java, there can be two possible cases:

  1. Method missing the return statement.
  2. Missing return statement error within if / while / for.
Java missing return statement

Let's understand both the cases one by one.

Method missing the return statement

In this case, we will get the missing return statement when the method uses the return type but doesn't return any value. Let's take an example to understand the concept of missing return statement error in method.

MissingReturnStatementExample1.java

Output

Java missing return statement

How to resolve the error?

There are two ways through which we can solve the error.

  1. By using void return type
  2. By adding a return statement
Java missing return statement

Both of these two ways are helpful only when we get this error in the method.

By using void return type

In the above code, we have created the diff() method in which we have declared return type as int. In this method, we didn't use the return statement, and due to which the diff() method throws the missing return statement. So, we will solve the problem by simply changing the return type to void without providing any return statement like as:

Let's compile and run the above code.

Java missing return statement

By adding a return statement

Another way of resolving this error is simply adding the return statement to the method. We will return the difference of the numbers using the return keyword like:

The above program shows the following output.

Output:

Java missing return statement

Missing return statement error within if / while / for

The second case is quite different from the first case. In a method, when we use the return statement inside the if statement, or for loop or while loop but doesn't use the return statement at the end of the method, it throws the missing return statement error.

Let's take an example to understand it.

MissingReturnStatementExample2.java

Output

Java missing return statement

How to resolve the error?

In order to solve the missing return statement error, we simply need to add the return statement to the method just like to the one we did in case one. So, we will return the some value of the same type which we used before the name like as:

Let's compile and run the above program.

Output:

Java missing return statement




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA