Search code examples
javaintellij-idea

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

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