Java – Intellij Idea recommends setting everything to final whenever possible, why?

Intellij Idea recommends setting everything to final whenever possible, why?… here is a solution to the problem.

Intellij Idea recommends setting everything to final whenever possible, why?

I’m a bit confused, my Intellij Idea would warn me if there are no final modifiers everywhere when possible.

Final is actually a good choice, and of course it is needed in many cases, such as method parameters, constants, and…,

But….

Why should my local method variable be final, there may be some very strong arguments?

Can you help me understand the benefits of this mass end-use?

Solution

One benefit of making all variables final is that it is clear which variables are assigned values only once. Another is that such a variable can be used in an anonymous class without having to make it final (since it is already final, but this benefit disappeared in Java 8).

Neither of the above benefits are very strong, so maybe you just want to disable local variables or parameters can be the final check. This check is disabled by default, so you might have accidentally enabled it?

Related Problems and Solutions