Why aren't local variables final in most open source Java projects?

If I look at the source code of a java source in OpenJDK or Hibernate or Apache, I have not yet seen any local variables being declared final.

This suggests that the developers of some of the most widely used Java software libraries:

  • Do not believe the last keyword improves readability.

  • they do not believe that this significantly improves performance.

Why do most contrbuters on stackoverflow think it should be used (based on the highest voice responses)?

+6
java final local-variables
source share
5 answers

don't believe the final keyword improves readability.

Some people (like me) find excessive final decreasing readability.

Do not consider this significant increase in productivity.

final Local variables do not improve performance.

+4
source share

Probably because it’s a hassle to type five LONG letters in the word final ... why they will experience pain when writing

 final int x; 

when it is twice as large as

 int x; 

?

We developers are lazy, you know ...: P

+5
source share

As far as I know, the final keyword does not affect the performance of your variables.

I believe that the main goal is to help you find errors. If you know that something will never change, you mark it as such. Similar to why we use annotations in which we can, at any time when we can trade a runtime error for a compile-time error, we do. Finding a mistake when you work on it, and it is fresher in your mind, and it did not disappear or spoil someone’s data because of which you lost customers, yes, that’s very good. You get a compilation error, you fix it, move on, you don’t break the nightly build, yes, these are good things.

+3
source share

The last keyword has two meanings:

  • declare a class or method final to prevent subclass / undo
  • declare the variable as final to prevent it from changing (assigning a new value)

Case 2 is usually applied to member variables to make an object immutable (at least partially) or by method parameters to avoid assignments.

In the case of a local variable (i.e. a method, but not a parameter), which is usually not needed or not needed, since these variables are likely to be changed inside the method (otherwise you may not need them, except for caching the reference for the scope method).

+2
source share

I doubt the local final variable ever improves performance. By virtue of the existence of final , Java compilers should already be able to determine whether a variable can be assigned more than once or cannot be initialized. Therefore, in fact declaring the local as final , it does not tell the compiler anything that it did not know yet - this is only in the interest of the reader.

Now, sometimes whether it improves readability, it is more subjective. In the complex part of the code, it can be nice to promise (for yourself or future readers) that the variable is written only once. But it might be better to simplify the code, so this is easy to understand.

+2
source share

All Articles