Because I believe it is a good programming practice, I make all my (local or instance) variables final
if they are intended to be written only once.
However, I notice that when a variable assignment can throw an exception you cannot make said variable final:
final int x;
try {
x = Integer.parseInt("someinput");
}
catch(NumberFormatException e) {
x = 42; // Compiler error: The final local variable x may already have been assigned
}
Is there a way to do this without resorting to a temporary variable? (or is this not the right place for a final modifier?)