简体   繁体   中英

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?

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!

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.

IntelliJ 2021.1 > Preferences/Settings > Editor > Inspections > Java > Code style issues > Local variable or parameter can be final 的截图

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM