Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Missing return statement" within if / for / while

Tags:

java

return

I have a question regarding return statements used within if() while() or for() statements.

As you can see in the following method, it is expecting that I return a String value. The problem is that if I were to use a return statement within my if statement block, the compiler would return the error missing return statement.

public String myMethod()
{
    if(condition)
    {
        return x;
    }
}

Of course I could change the method header to void and use System.out.println instead of return. But is this the right way to do it? Am I missing something?

like image 405
user3531560 Avatar asked Apr 14 '14 10:04

user3531560


People also ask

How do I fix missing return statement in Java?

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: public static String checkNumber( int number) {

What happens if a user defined function is missing the return statement?

The “missing return statement” message occurs when a method does not have a return statement. Each method that returns a value (a non-void type) must have a statement that literally returns that value so it can be called outside the method.

Can we use return statement in IF condition?

When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. For example, the following function returns the square of its argument, x , where x is a number. If the value is omitted, undefined is returned instead.


2 Answers

If you put a return statement in the if, while or for statement then it may or may not return a value. If it will not go inside these statements then also that method should return some value (that could be null). To ensure that, compiler will force you to write this return statement which is after if, while or for.

But if you write an if / else block and each one of them is having a return in it then the compiler knows that either the if or else will get executed and the method will return a value. So this time the compiler will not force you.

if(condition)
{
    return;
}
else
{
    return;
}
like image 56
Bhushan Kawadkar Avatar answered Sep 18 '22 14:09

Bhushan Kawadkar


That's because the function needs to return a value. Imagine what happens if you execute myMethod() and it doesn't go into if(condition) what would your function return? The compiler needs to know what to return in every possible execution of your function.

Checking Java documentation:

Definition: If a method declaration has a return type then there must be a return statement at the end of the method. If the return statement is not there the missing return statement error is thrown.

This error is also thrown if the method does not have a return type and has not been declared using void (i.e., it was mistakenly omitted).

You can do this to solve your problem:

public String myMethod()
{
    String result = null;
    if(condition)
    {
        result = x;
    }
    return result;
}
like image 45
Guillermo Merino Avatar answered Sep 19 '22 14:09

Guillermo Merino