3 ms·
> `final` doesn't buy you as much in Java as `const` does in C, or (the lack of) `mut` in Rust. Quite true. > `final` only helps you avoid dumb reassignment b
by AdieuToLogic 11y ago
> `final` doesn't buy you as much in Java as `const` does in C, or (the lack of) `mut` in Rust.
Quite true.
> `final` only helps you avoid dumb reassignment bugs that tests should probably catch anyway ...
There is another reason for declaring parameters `final` in Java. It is the only way[1] which they can be used in an anonymous class definition such as:
return new SomeClass () {
override int addOne () {
return aNamedOuterMethodParam + 1;
}
}
1 - "The only way" as of Java 7. Newer versions may have removed this restriction, I haven't bothered to check.