Piotr Pradzynski
Piotr Pradzynski

Reputation: 4535

How to setup IntelliJ IDEA 14 to add "final" keyword where possible?

I would like to setup my IntelliJ IDEA 14 for automatic final keyword adding in all possible places (parameters, fields, etc.) to the existing Java class. I found some solutions with macros and shortcuts but I would like setup IntelliJ to add the final keyword when it saves the file. Does anyone know a good solution for this? Maybe can you recommend some plugin or combination of plugins?

Upvotes: 34

Views: 18420

Answers (3)

Egor Klepikov
Egor Klepikov

Reputation: 5909

Preferences - Code style issues

Since the 2018.3 version we can enable the checkbox:

Preferences/Settings > Editor > Inspections > Java > Code style issues > Local variable or parameter can be final

Then perform the reformat code action.

If the Code cleanup checkbox is enabled then IDEA will automatically add final where possible.

screenshot of IntelliJ 2021.1 > Preferences/Settings > Editor > Inspections > Java > Code style issues > Local variable or parameter can be final

Upvotes: 34

kolobok_ua
kolobok_ua

Reputation: 4220

Use Save Actions plugin: https://plugins.jetbrains.com/plugin/7642-save-actions

You can enable adding of final keywords in:

Settings -> Other Settings -> Save Actions -> Add final to field/local variable`.

Upvotes: 10

Steve Chaloner
Steve Chaloner

Reputation: 8202

Open the "Configure inspections" view (click on the person in the hat icon). Search on "final" and you'll find various options in the code style section that you can enable to enable highlighting of candidates for final. You can then use alt-return to apply the suggestion within a given file.

I wouldn't recommend you do it automatically on saving!

Upvotes: 14

Related Questions