“Missing return statement” within if / for / while

后端 未结 7 1117
粉色の甜心
粉色の甜心 2020-11-22 02:02

I have a question regarding return statements used within if() while() or for() statements. As you can see in the following method, it

7条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 02:29

    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.

提交回复
热议问题