57
votes

In Java, you can qualify local variables and method parameters with the final keyword.

public static void foo(final int x) {
  final String qwerty = "bar"; 
}

Doing so results in not being able to reassign x and qwerty in the body of the method.

This practice nudges your code in the direction of immutability which is generally considered a plus. But, it also tends to clutter up code with "final" showing up everywhere. What is your opinion of the final keyword for local variables and method parameters in Java?

1

12 Answers 12

53
votes

You should try to do this, whenever it is appropriate. Besides serving to warn you when you "accidentally" try to modify a value, it provides information to the compiler that can lead to better optimization of the class file. This is one of the points in the book, "Hardcore Java" by Robert Simmons, Jr. In fact, the book spends all of its second chapter on the use of final to promote optimizations and prevent logic errors. Static analysis tools such as PMD and the built-in SA of Eclipse flag these sorts of cases for this reason.

10
  • 8
    +1: Just one addition - final also convey's the author's intent, both in parameters and local variables.
    – Ken Gentle
    Nov 25, 2008 at 12:58
  • 3
    I'm reading Hardcore Java right now. I thought I knew java........... Dec 9, 2008 at 21:25
  • 2
    Are you sure final locals/method parameters can cause the compiler to optimize better? For a counter point, imagine if you declare a local variable int x = 2. If you unconditionally never reassign x, the compiler will know this, because by scanning the entire code body and seeing that x cannot possibly be reassigned no matter what condition, it deducts that x will never be modified... Jun 27, 2010 at 23:51
  • 5
    Ironic that PMD doesn't use final method parameters in their own source code, to give one example. Nor does most of the JDK that I've looked at. Nor do the Eclipse refactorings for, say, encapsulation. Oct 3, 2010 at 23:44
  • 1
    @Yar many Eclipse refactorings and source generation/completion templates are not examplary... Nov 18, 2010 at 12:32
27
votes

My personal opinion is that it is a waste of time. I believe that the visual clutter and added verbosity is not worth it.

I have never been in a situation where I have reassigned (remember, this does not make objects immutable, all it means is that you can't reassign another reference to a variable) a variable in error.

But, of course, it's all personal preference ;-)

1
  • 5
    +1 because I think that it's not only about the method body and logic errors (into which I've never ran just because of non-final variables, FWIW), but also about readable method signatures. I can't see how a parameter declared as final makes the signature more readable. Simple types are passed by values, so are not modifiable. Complex types are passes by reference, but the reference is passed by value, so is not modifiable. Final is just noise for a reader of the signature.
    – mxk
    Jul 18, 2010 at 11:17
8
votes

Making a parameter final guarantees that the value used at any location in the method refers to the value passed. Otherwise you have to parse mentally all the code above a given location to know what value the parameter has at that point.

Hence, not using final makes your code less readable, and maintainable, all by itself :)

Final local variables depend on intent, and is less important in my point of view. Depends on what goes on.

2
  • Please beware that final only marks the reference as immutable. The properties of the referenced object can change at any time. Thus you'll only gain this level of assurance if you make the whole object graph immutable. This is nevertheless a very good practice even thow it's hard to achieve in java. Nov 21, 2011 at 9:41
  • It guarantees that the object referred to is the same. The contents of said object may, naturally, change. Nov 21, 2011 at 9:56
6
votes

In the case of local variables, I tend to avoid this. It causes visual clutter, and is generally unnecessary - a function should be short enough or focus on a single impact to let you quickly see that you are modify something that shouldn't be.

In the case of magic numbers, I would put them as a constant private field anyway rather than in the code.

I only use final in situations where it is necessary (e.g., passing values to anonymous classes).

5
votes

Because of the (occasionally) confusing nature of Java's "pass by reference" behavior I definitely agree with finalizing parameter var's.

Finalizing local var's seems somewhat overkill IMO.

2
  • But if you pass a List by reference and mark it as "final" in the parameter list, you can still add or remove elements and see them reflected in the caller - this is surely MORE confusing than not putting "final" on the parameter in this case?
    – mjaggard
    Jan 30, 2012 at 14:46
  • Not necessarily. Object mutability is a fairly novice concept IMO. Where I work, we expect devs to understand that a final Object does not mean that it can't change, just that it can't be re-referenced. Jan 30, 2012 at 16:15
3
votes

Yes do it.

It's about readability. It's easier to reason about the possible states of the program when you know that variables are assigned once and only once.

A decent alternative is to turn on the IDE warning when a parameter is assigned, or when a variable (other than a loop variable) is assigned more than once.

4
  • finalized wont preserve a parameter or variable from become modified, it is not like const in C++. Pass list to a final parameter an you can clear it anyway. Jan 3, 2009 at 20:29
  • It's not about safety, it's about readability. Obviously making a parameter final has no effect on which states are possible at the end of the method. Jan 5, 2009 at 16:24
  • 2
    @Motlin: it does for primitives and immutable objects, which covers a lot of parameters in my experience. Feb 12, 2009 at 22:07
  • 1
    @Andrew I'm talking about state on the heap, not on the stack which is about to get lost anyway. There are no side effects that are possible with non-final parameters that you can't do with final parameters. Feb 12, 2009 at 23:11
2
votes

final has three good reasons:

  • instance variables set by constructor only become immutable
  • methods not to be overridden become final, use this with real reasons, not by default
  • local variables or parameters to be used in anonimous classes inside a method need to be final

Like methods, local variables and parameters need not to be declared final. As others said before, this clutters the code becoming less readable with very little efford for compiler performace optimisation, this is no real reason for most code fragments.

2
votes

Although it creates a little clutter, it is worth putting final. Ides e.g eclipse can automatically put the final if you configure it to do so.

2
votes

Making local variables and method parameters final is essential if you want to pass those parameters into anonymous classes - like you instantiate an anonymous Thread and want to access those params in the body of the run() method.

Apart from that I am not sure of the performance benefits w.r.t better performance through compiler optimization. It is up to the specific compiler implementation whether it wants to optimize it at all...

It will be good to know of any performance stats from using final ...

0
votes

Why would you want to? You wrote the method, so anyone modifying it could always remove the final keyword from qwerty and reassign it. As for the method signature, same reasoning, although I'm not sure what it would do to subclasses of your class... they may inherit the final parameter and even if they override the method, be unable to de-finalize x. Try it and find out if it would work.

The only real benefit, then, is if you make the parameter immutable and it carries over to the children. Otherwise, you're just cluttering your code for no particularly good reason. If it won't force anyone to follow your rules, you're better off just leaving a good comment as you why you shouldn't change that parameter or variable instead of giving if the final modifier.

Edit

In response to a comment, I will add that if you are seeing performance issues, making your local variables and parameters final can allow the compiler to optimize your code better. However, from the perspective of immutability of your code, I stand by my original statement.

5
  • The JRE or compiler can do more optimization if it knows the object is finalized.
    – paxdiablo
    Nov 25, 2008 at 4:25
  • Sure, but that wasn't the question. In terms of making your code immutable, it doesn't really work. However, you are quite correct in that it can be slightly faster than not using it, and should be considered when performance is an issue.
    – Elie
    Nov 25, 2008 at 4:27
  • As I commented on Pax's answer, I'm fairly sure there are no perf improvements by making local variables final, since the JVM can guess that for you.
    – SCdF
    Nov 25, 2008 at 4:31
  • No, just compiler improvements, but who cares about those. Now I'm too lazy to correct myself yet again. Oh well....
    – Elie
    Nov 25, 2008 at 4:34
  • Finalizing the parameter is not an optimization. The final flag is stripped out at compile time. It is only used by the compiler, not at runtime.
    – Robin
    Nov 25, 2008 at 14:32
0
votes

I let Eclipse do it for me when they are being used in an anonymous class, which is increasing due to my use of Google Collection API.

0
votes

We do it here for the local variables if we think they will not be reassigned or should not be reassigned.

The parameters are not final since we have a Checkstyle-Check which checks for reassigning parameters. Of course nobody would ever want to reassign a parameter variable.

Not the answer you're looking for? Browse other questions tagged or ask your own question.