which would you say is the best practice when implementing the following problem:
MyClass myVariable = null;
if ( ..condition 1.. ) {
myVariable = new MyClass(1);
} else if ( ..condition 2.. ) {
myVariable = new MyClass(2);
}
myVariable.execute();
Which would be a good solution to the warning?
A finishing
else
final MyClass myVariable; .... } else { // let's say this assert makes sense here Assert.fail("This should not happen"); }
throw
RuntimeException
final MyClass myVariable; .... } else { throw new RuntimeException("Some message, like <should not happen>"); }
Check for NPE
final MyClass myVariable; .... if (myVariable != null) { myVariable.execute(); }
Other ideas?
Thanks in advance!