I have a question regarding return statements used within if()
while()
or for()
statements.
As you can see in the following method, it
This will return the string only if the condition is true.
public String myMethod()
{
if(condition)
{
return x;
}
else
return "";
}
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;
}
If you put return statement in if
, while
or for
statement then it may or may not return value. If it will not go inside these statement 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 if
/ else
block and each one of them is having return in it then compiler knows that either if
or else
will get execute and method will return a value. So this time compiler will not force you.
if(condition)
{
return;
}
else
{
return;
}
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
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.
}
Any how myMethod() should return a String value .what if your condition is false is myMethod return anything? ans is no so you need to define return null or some string value in false condition
public String myMethod() {
boolean c=true;
if (conditions) {
return "d";
}
return null;//or some other string value
}