“Missing return statement” within if / for / while

后端 未结 7 982
粉色の甜心
粉色の甜心 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.

    0 讨论(0)
提交回复
热议问题