I know how to use final to set an int, like this:
final int junk = whatever ? 1 : 2;
But, how to do this in more complicated if statements?
Example:
final int junk;
if(whatever) {
junk = 1;
} else {
junk = 2;
}
You can nest final value setting to any depth, and Java will flag an error if you make any duplicates or skip any paths.
Example:
final int junk;
if(whatever) {
junk = 1;
} else {
if(however) {
junk = 2;
} else {
junk = 3;
}
}
I use final
for local variable closure whenever possible to insure that I don't accidentally reuse variables unexpectedly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With