Auto-add the "final" modifier to newly declared variables/values

How can I auto-add the final modifier to newly declared variables? What I want is to write

int i = 42;

and having this variable automatically converted to a value using the final modifier, so this line would look like:

final int i = 42;


Settings | Code Style | Java | Code Generation | Make generated local variables final.

You could find this option by typing 'final' in the search box of the Settings dialog.


For your example workflow is following:
1. type 42
2. Invoke 'Extract variable' (Ctrl+Alt+V on Windows, Meta+Alt+V on Mac)
As a result you get final int i = 42;


Thanks a lot for the quick reply!

The "Make generated * final" I've found and checked myself but I expected a more automated behavior. Alternatively I would like to make all possible variables final by invoking an action. The point is, I need to mimic Eclipse' behavior (probably it's a hook provided by a 3rd-party plugin) to make all possible variables final when you save a file (or in IntelliJ' case performing any other action like "Reformat code").


Enable Settings | Inspections | Code Style Issues | Local variable or parameter can be final. Non-final vars will get warning gighlighting. Naviagate to any and press Alt+Enter. At 'Accept Suggested Final Modifier' press Right arrow and select Fix all 'Local variable or parameter can be final' problems. This works per file. To find all such problems in the project use Analyze | Run inspection by name.


Thanks, this comes much closer to what I've looked for!


Check this plugin, maybe this is what you're looking for.


Please sign in to leave a comment.