I can't understand where the final
keyword is really handy when it is used on method parameters.
If we exclude the usage of anonymous classes, readability and intent declaration then it seems almost worthless to me.
Enforcing that some data remains constant is not as strong as it seems.
-
If the parameter is a primitive then it will have no effect since the parameter is passed to the method as a value and changing it will have no effect outside the scope.
-
If we are passing a parameter by reference, then the reference itself is a local variable and if the reference is changed from within the method, that would not have any effect from outside of the method scope.
Consider the simple test example below.
This test passes although the method changed the value of the reference given to it, it has no effect.
public void testNullify() {
Collection<Integer> c = new ArrayList<Integer>();
nullify(c);
assertNotNull(c);
final Collection<Integer> c1 = c;
assertTrue(c1.equals(c));
change(c);
assertTrue(c1.equals(c));
}
private void change(Collection<Integer> c) {
c = new ArrayList<Integer>();
}
public void nullify(Collection<?> t) {
t = null;
}
Best Answer
Stop a Variable’s Reassignment
While these answers are intellectually interesting, I've not read the short simple answer:
Whether the variable is a static variable, member variable, local variable, or argument/parameter variable, the effect is entirely the same.
Example
Let’s see the effect in action.
Consider this simple method, where the two variables (arg and x) can both be re-assigned different objects.
Mark the local variable as final. This results in a compiler error.
Instead, let’s mark the parameter variable as final. This too results in a compiler error.
Moral of the story:
Never Reassign Arguments
As good programming practice (in any language), you should never re-assign a parameter/argument variable to an object other than the object passed by the calling method. In the examples above, one should never write the line
arg =
. Since humans make mistakes, and programmers are human, let’s ask the compiler to assist us. Mark every parameter/argument variable as 'final' so that the compiler may find and flag any such re-assignments.In Retrospect
As noted in other answers… Given Java's original design goal of helping programmers to avoid dumb mistakes such as reading past the end of an array, Java should have been designed to automatically enforce all parameter/argument variables as 'final'. In other words, Arguments should not be variables. But hindsight is 20/20 vision, and the Java designers had their hands full at the time.
So, always add
final
to all arguments?Should we add
final
to each and every method parameter being declared?➥ Add
final
only when the method’s code is long or complicated, where the argument may be mistaken for a local or member variable and possibly re-assigned.If you buy into the practice of never re-assigning an argument, you will be inclined to add a
final
to each. But this is tedious and makes the declaration a bit harder to read.For short simple code where the argument is obviously an argument, and not a local variable nor a member variable, I do not bother adding the
final
. If the code is quite obvious, with no chance of me nor any other programmer doing maintenance or refactoring accidentally mistaking the argument variable as something other than an argument, then don’t bother. In my own work, I addfinal
only in longer or more involved code where an argument might mistaken for a local or member variable.#Another case added for the completeness
record
Java 16 brings the new records feature. A record is a very brief way to define a class whose central purpose is to merely carry data, immutably and transparently.
You simply declare the class name along with the names and types of its member fields. The compiler implicitly provides the constructor, getters,
equals
&hashCode
, andtoString
.The fields are read-only, with no setters. So a
record
is one case where there is no need to mark the argumentsfinal
. They are already effectively final. Indeed, the compiler forbids usingfinal
when declaring the fields of a record.If you provide an optional constructor, there you can mark
final
.