"Missing return statement" inside if / for / while

I have a question regarding return statements used in if() while() or for() operations. As you can see in the following method, it expects me to return a String value. The problem is that if i, where to use return in my if block, the compiler will return a missing return statement error.

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

Of course, I can change the method title to void and use System.out.println instead of return . But is it right to do this? Did I miss something?

Any help is appreciated.

+7
java return
source share
10 answers

If you put a return statement in an if , while or for if , then it may or may not return a value. If it does not go inside this statement, then this method should also return some value (which may be null). To make sure that the compiler forces you to write this return statement, which is after if , while or for .

But if you write an if / else block, and each of them has a return in it, the compiler knows that either if or else will receive execute and the method will return a value. Therefore, this time compiler will not force you.

 if(condition) { return; } else { return; } 
+5
source share

What is required for a function to return a value. Imagine what happens if you execute myMethod() and it does not go into if(condition) so that your function returns? The compiler should know what to return at every possible execution of your function.

Check Java documentation:

Definition If the method declaration has a return type, then you must be a return statement at the end of the method. If the return does not exist, an error return is returned.

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

You can solve your problem:

 public String myMethod() { String result = null; if(condition) { result = x; } return result; } 
+3
source share

Try as if if condition returns false, so it will return empty, otherwise nothing will be returned.

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

Since the compiler does not know if any of these if blocks will ever be reached, this will result in an error.

+2
source share

This is illegal syntax . This is not an extra thing to return a variable. You MUST return a variable of the type specified in your method.

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

You effectively say, I promise that any class can use this method (public), and I promise that it will always return String (String).

Then you say that my condition is true, I will return x. Well, this 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 must assume that it is likely that it is false. Therefore, you always need to return at the end of your non-void method outside of any conditions, ONLY IN THE CASE that 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. } 
+2
source share

This is because if you don’t go to if, you won’t be able to return anything, so it will skip the return. :)

it should be:

 public String myMethod() { if(condition) { return x; } return y; } 
0
source share

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

 public String myMethod() { if(condition) { return x; } else return ""; } 
0
source share

try the following:

 public String myMethod() { if(condition) { return x; } return ""; //returns empty string } 
0
source share
 public String myMethod() // it ALWAYS expects a String to be returned { if(condition) // will not execute always. Will execute only when condition is true { return x; // will not be returned always. } //return empty string here } 
0
source share

You must add a return statement if 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

0
source share

Anyone like myMethod () should return a String value. What if your condition is false, does myMethod return anything? ans wrong, you need to define return null or some string value in false state

 public String myMethod() { boolean c=true; if (conditions) { return "d"; } return null;//or some other string value } 
0
source share

All Articles