Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
1  
@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
show 10 more comments

4 Answers

up vote 16 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
add comment

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
add comment

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
2  
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
2  
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
add comment

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
    
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
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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