“Missing return statement” within if / for / while

2018-12-31 07:26发布

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?

Any help is highly appreciated.

标签: java return
10条回答
深知你不懂我心
2楼-- · 2018-12-31 07:57

Try with, as if 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.

查看更多
听够珍惜
3楼-- · 2018-12-31 07:58

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 is no 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.
}
查看更多
步步皆殇っ
4楼-- · 2018-12-31 08:03

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 returns? 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 to solve your problem:

public String myMethod()
{
    String result = null;
    if(condition)
    {
       result = x;
    }
    return result;
}
查看更多
泪湿衣
5楼-- · 2018-12-31 08:03

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

public String myMethod()
{
    if(condition)
    {
       return x;
    }
    else
       return "";
}
查看更多
闭嘴吧你
6楼-- · 2018-12-31 08:12

try this:

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

   return ""; //returns empty string
}
查看更多
春风洒进眼中
7楼-- · 2018-12-31 08:16

It's because if you don't go in the if, there is nothing to return, so it miss a return. :)

should be :

public String myMethod()
{
    if(condition)
    {
       return x;
    }
    return y;
}
查看更多
登录 后发表回答