21

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3531560
  • 251
  • 1
  • 2
  • 7
  • 8
    What happens if the condition is not true? What does your method return in that case? – devnull Apr 14 '14 at 10:44
  • You could return a default value (like null), if the value is not found. When calling the method you check if the value is equal to the default value and can thus check if the returned value should be processed further. – Pphoenix Apr 14 '14 at 10:45
  • you should add a `return` statement after the `if` or in an `else` statement – Laabidi Raissi Apr 14 '14 at 10:45
  • 1
    Every code path needs to return; here the only code path which returns something is if `condition` is true. – fge Apr 14 '14 at 10:46
  • possible duplicate of [Missing return statement } in java error](http://stackoverflow.com/questions/20709459/missing-return-statement-in-java-error) – devnull Apr 14 '14 at 10:46

7 Answers7

15

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
  • 2
    I think overall he should just learn that he needs to put a return right before the last bracket of every non-void method. Because he is going to rephrase his question with that else surrounded in a while next time. – CodeCamper Apr 14 '14 at 11:00
  • I'd say that using multiple return statements is not a solution. It is better to have only one `return` statement at the end of the function/method. – VP. Apr 14 '14 at 11:19
6

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
5

That is illegal syntax. It is not an optional thing for you to return a variable. You must return a variable of the type you specify in your method.

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

You are effectively saying: I promise any class can use this method (public) and I promise it will always return a String (String).

Then you are saying IF my condition is true I will return x. Well, that is too bad. There isn't any IF in your promise. You promised that myMethod will always return a String.

Even if your condition is always true, the compiler has to assume that there is a possibility of it being false. Therefore you always need to put a return at the end of your non-void method outside of any conditions just in case all of your conditions fail.

public String myMethod()
{
    if(condition)
    {
        return x;
    }
    return ""; // Or whatever the default behavior will be if all of your conditions fail to return.
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CodeCamper
  • 6,609
  • 6
  • 44
  • 94
  • 1
    (Correction: this is NOT a syntax error / "illegal syntax". It is a compilation error that has to do with semantics rather than syntax. A "syntax" error means that the program doesn't conform to the language's grammar, but there is no production in the Java grammar that makes a return statement mandatory.) – Stephen C Apr 06 '19 at 02:00
3

Try with, as if the if condition returns false, so it will return empty, otherwise nothing to return.

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

Because the compiler doesn't know if any of those if blocks will ever be reached, so it's giving you an error.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bNd
  • 7,512
  • 7
  • 39
  • 72
1

This will return the string only if the condition is true.

public String myMethod()
{
    if(condition)
    {
        return x;
    }
    else
        return "";
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
1

You have to add a return statement if the condition is false.

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

//  if condition is false you HAVE TO return a string
//  you could return a string, a empty string or null
    return otherCondition;  
}

FYI:

Oracle docs for return statement

d3vnico
  • 93
  • 8
1

Anyhow, myMethod() should return a String value. What if your condition is false? - is myMethod returning anything? The answer is no, so you need to define return null or some string value for the false condition:

public String myMethod() {
    boolean c = true;
    if (conditions) {
        return "d";
    }
    return null; // Or some other string value
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
loknath
  • 1,362
  • 16
  • 25