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?