Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Java allows marking variables (fields / locals / parameters) as final, to prevent re-assigning into them. I find it very useful with fields, as it helps me quickly see whether some attributes - or an entire class - are meant to be immutable.

On the other hand, I find it a lot less useful with locals and parameters, and usually I avoid marking them as final even if they will never be re-assigned into (with the obvious exception when they need to be used in an inner class). Lately, however, I've came upon code which used final whenever it can, which I guess technically provides more information.

No longer confident about my programming style, I wonder what are other advantages and disadvantages of applying final anywhere, what is the most common industry style, and why.

share|improve this question
    
@Amir coding-style questions just seem to belong here better than on SO, and I couldn't find any policy in the FAQ or on this site's meta regarding this. Can you please direct me? – Oak Feb 16 '11 at 9:12
1  
@Oak It says: "Specific programming problem, software algorithms, coding, ask on Stack Overflow" – Amir Rezaei Feb 16 '11 at 9:20
4  
@Amir I disagree, I don't see how this is a coding problem. In any case this debate does not belong here, so I've opened a meta-topic on this issue. – Oak Feb 16 '11 at 9:32
    
I find myself wishing C# had something equivalent because it would make some manual refactoring much easier. – Peter Taylor Feb 16 '11 at 9:47
3  
@amir this is totally on topic for this site, as it is a subjective question about programming -- please see the /faq – Jeff Atwood Feb 16 '11 at 18:03
up vote 32 down vote accepted

I use final the same way as you. To me it looks superfluous on local variables and method parameters, and it doesn't convey useful extra information.

One important thing is that strive to keep my methods short and clean, each doing a single task. Thus my local variables and parameters have a very limited scope, and are used only for a single purpose. This minimizes the chances of reassigning them inadvertently.

Moreover, as you surely know, final doesn't guarantee that you can't change the value/state of a (nonprimitive) variable. Only that you can't reassign the reference to that object once initialized. In other words, it works seamlessly only with variables of primitive or immutable types. Consider

final String s = "forever";
final int i = 1;
final Map<String, Integer> m = new HashMap<String, Integer>();

s = "never"; // compilation error!
i++; // ditto
m.put(s, i); // fine

This means that in many cases it still doesn't make it easier to understand what happens inside the code, and misunderstanding this may in fact cause subtle bugs which are hard to detect.

share|improve this answer
    
Regarding the edit - I'm aware of the semantics of final, thank you :) but good point about short & clean methods - I guess that if the method is short enough that it's obvious a variable isn't being re-assigned into, there's even less motive for considering the final keyword. – Oak Feb 16 '11 at 10:35
    
Wouldn't it be great if we could have final parameters & local variables, and still a short and clean syntax? programmers.stackexchange.com/questions/199783/… – oberlies May 29 '13 at 13:05
    
"final doesn't guarantee that you can't change the value/state of a (nonprimitive) variable. Only that you can't reassign the reference to that object once initialized." Nonprimitive = reference (the only types in Java are primitive types and reference types). The value of a variable of reference type is a reference. Therefore, you can't reassign the reference = you can't change the value. – user102008 Dec 5 '15 at 0:12

One advantage of using final / const wherever possible is that it reduces the mental load for the reader of your code.

He can be rest assured, that the value / reference is never altered later on. So he does not need to pay attention to modifications in order to understand the computation.

I've have changed my mind regarding this after learning pure-functional programming languages. Boy, what a relieve, if you can trust a "variable" to always hold its initial value.

share|improve this answer
4  
Well, in Java final doesn't guarantee that you can't change the value/state of a (nonprimitive) variable. Only that you can't reassign the reference to that object once initialized. – Péter Török Feb 16 '11 at 9:35
3  
I know, that's why i distinguished between value and reference. The concept is the most useful in the context of immutable data structures and/or pure-functionality. – LennyProgrammers Feb 16 '11 at 11:07

Your Java programming style and thoughts are fine - don't need to doubt yourself there.

On the other hand, I find it a lot less useful with locals and parameters, and usually I avoid marking them as final even if they will never be re-assigned into (with the obvious exception when they need to be used in an inner class).

This is exactly why you should use the final keyword. You state that YOU know it'll never be re-assigned, but no one else knows that. Using final immediately disambiguates your code that tiny bit more.

share|improve this answer

I let eclipse put final before each local variable, since I consider it to make the program easier to read. I don't make it with parameters, since I want to keep the parameter list as short as possible, ideally it should fit in one line.

share|improve this answer
1  
Also, for parameters, you can have the compiler issue a warning or error if a parameter is assigned. – oberlies May 29 '13 at 13:03
1  
Quite the opposite, I find it harder to read as it just clutters up the code. – Steve Kuo Nov 2 '13 at 15:32
1  
@Steve Kuo: It just allows me to quickly find all variables. no big gain, but together with preventing accidental assignments it's worth the 6 chars. I'd be much more happy if there was something like var for marking non-final variables. YMMV. – maaartinus Nov 12 '13 at 11:26

If you want to ensure a variable always points to the same object, mark the variable final. as mentioned by @BasilBourque on Stack Overflow

share|improve this answer
    
What does this answer say that was not already said in the existing four answers from four and a half years ago? – Snowman Dec 4 '15 at 16:01
    
@Snowman , I added in concise statement of what final keyword means in Java. To my surprise this question hasn't been closed yet!! Beside that I was linking this question to best answer about final from stackoverflow, down voting seems to be harsh, doesn't matter to me much though. – Abhijeet Dec 4 '15 at 16:49
    
This question may appear a bit opinion-based but it really is not. The final keyword has real, measurable, and objective consequences that can be beneficial. This is a "good subjective" question. I would also caution against link-only answers although you did quote part of the target. – Snowman Dec 4 '15 at 17:42
    
@Snowman , " The final keyword has real, measurable, and objective consequences that can be beneficial. " that's what I have highlighted in If you want to ensure a variable always points to the same object, mark the variable final. line. Anyways thanq for the Caution link . – Abhijeet Dec 5 '15 at 2:55
    
Again, what does this answer say that was not already said in the existing four answers from four and a half years ago? – Snowman Dec 5 '15 at 4:38

protected by Snowman Dec 4 '15 at 16:01

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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