28

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?

Piotr Pradzynski
  • 4,190
  • 5
  • 23
  • 43
  • I would not recommend even trying this. Just because it is currently possible, doesn't mean it is supposed to be so. Meaning, you'll probably spend quite some time afterwards solving ye olde 'trying to re-reference final variable' errors. – Stultuske Apr 27 '15 at 09:11
  • 2
    @Stultuske, I'm just asking whether (and how) it is possible to setup this functionality on save. As far as remember Eclipse has similar functionality on save and I'm searching same functionality in IntelliJ. – Piotr Pradzynski Apr 27 '15 at 10:15
  • 2
    Disagree with @Stultuske - I strongly believe it's better to favor immutability over mutability. Especially in a team/corporate environment, it's much better and safer to know when you are choosing mutability than to have variables left mutable when you aren't expecting that behavior. – hiljusti Mar 31 '18 at 22:46
  • @JustinHill which is a perfect way to work, if you only use classes developped in house. – Stultuske Apr 03 '18 at 06:14

3 Answers3

24

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Egor Klepikov
  • 3,386
  • 5
  • 19
  • 34
14

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!

Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
  • 12
    I'm just asking whether (and how) it is possible to setup this functionality on save. As far as remember Eclipse has similar functionality on save and I'm searching same functionality in IntelliJ. – Piotr Pradzynski Apr 27 '15 at 10:16
  • 1
    He was asking for how to do it, not whether you agree w/ him or not. – Ogmios Jan 26 '21 at 00:56
10

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`.

kolobok
  • 3,835
  • 3
  • 38
  • 54