2 ms·
True. When in Java, I've gotten into the habit of using the final initializer to mitigate these kinds of issues, like this: final T var1; if (cond) {
by macspoofing 8y ago
True.
When in Java, I've gotten into the habit of using the final initializer to mitigate these kinds of issues, like this:
final T var1;
if (cond) {
var1 = x;
} else if (cond2) {
var2 = y;
} else {
var1 = z;
}
In this specific case, the compiler will catch the lack of initialization in the second block. It's also very helpful with a complicated/nested conditionals to guarantee that you initialize the variable through every code path.