Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 972
StKiller Avatar asked Mar 06 '12 07:03

StKiller


People also ask

When a variable is declared with the final keyword?

A final variable is called a blank final variable if it is not initialized while declaration. Below are the two ways to initialize a blank final variable. A blank final variable can be initialized inside an instance-initializer block or inside the constructor.


1 Answers

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.

like image 103
Jake Walsh Avatar answered Sep 21 '22 15:09

Jake Walsh