Intellij Idea - automatically add final keyword to the generated variables

The new introduced code convention require to use the "final" keyword where possible.

The question is - is there a possibility to automatically generate the variables with this keyword ? For example - I select a code fragment:

"bla"+"bla"+"bla"

and press ctrl+alt+v - the IDE generates a variable like this:

String blaString = "bla"+"bla"+"bla";

but I need it to be

final String blaString = "bla"+"bla"+"bla";

P.S. I know about inspections and the possibility "fix them all", but it can't be used everywhere as there is a lot of old code that shouldn't be refactored without reason.


Solution 1:

Go to preferences -> Code Style -> Code Generation and enable "Make generated local variables final" in the "Final modifier" section. You can also try searching for "final" in preferences.

Solution 2:

In IntelliJ 14.1.3

  1. Preferences > Editor > Code Style > Java
  2. To the far right ,there's a tab called Code Generation
  3. In the section called Final Modifier there are two choices:
    • Make generated local variables final
    • Make generated parameters final