Intellij Idea - automatically adds the final keyword to the generated variables

The new code must use the "final" keyword, where possible.

Question: is it possible to automatically generate variables with this keyword? For example - I select a piece of code:

"bla"+"bla"+"bla" 

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

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

but I need it to be

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

PS I know about inspections and the possibility of "fixing them all", but it cannot be used everywhere, since there are a lot of old code that cannot be reorganized for no reason.

+72
java standards intellij-idea
Mar 06 '12 at 7:11
source share
3 answers

Go to Settings โ†’ Code Style โ†’ Code Generation and turn on โ€œMake generated local variables finalโ€ in the โ€œFinal modifierโ€ section. You can also try the โ€œfinalโ€ in the settings.

+112
Mar 06 '12 at 7:28
source share

IntelliJ 14.1.3

  • Preferences > Editor > Code Style > Java
  • In the far right corner is the Code Generation tab
  • There are two options in the Final Modifier section:
    • Make generated local variables final
    • Make ready-made parameters final
+42
Jun. 19 '15 at 15:09
source share

For Windows user;

Go to File โ†’ Settings โ†’ Editor โ†’ Code Style โ†’ Choose your preferred language ( Java ) โ†’ Checkbox โ†’ Make generated local variables final, Make generated parameters final -> Apply โ†’ OK

+3
Jan 19 '17 at 8:59
source share



All Articles