Consider the following method:
private static String method (String string) {
if (string.equals("conditionOne")) {
return value;
} else if (string.equals("conditionTwo")) {
return symbol;
} else {
return null;
}
}
Let's say I am checking for two conditions, conditionOne
and conditionTwo
. Also, assume that some other part of the program ensures that only these two cases will ever happen. Since the method has to return something for all cases to avoid a compiler error, is it okay to return null
for the final else
block just for syntactical purposes since that part will never execute?
Edit: For clarity, I'd like to mention that the compiler gives me an error ("Expecting return statement") if I don't include that last else
block. Other than to returning null (or an empty string, as pointed out by Anthony below), is there another way to write this method so that this does not happen?
Thanks