Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have Eclipse automatically make all my variables final?

Tags:

I try to avoid having non-final variables when possible, and it would save some typing if Eclipse would automate this for me (also so I don't forget to do it). What I'd like is for it to detect when I add a new variable assignment/declaration statement, and as soon as I finish it, insert the final modifier.

Is there a configuration option or lightweight plugin that would accomplish this?

like image 571
Daenyth Avatar asked Jul 09 '12 15:07

Daenyth


People also ask

How do I turn on autocorrect in Eclipse?

In the Window > Preferences menu, you can search for spell checking (located in General > Editors > Text Editors > Spelling) and there you can edit your spell checking options.

How do I change occurrences in Eclipse?

Press ctrl + f for windows computer. Then under FIND, type what you want to find. Then in REPLACE WITH, type in what you want to replace it with. Then click REPLACE ALL.

How do I change multiple variables in Eclipse?

Select the variable you with to change, right click, and select Refactor->Rename. This will highlight all instances of the variable. Changing the variable name is automatically propagated to all of the instances. Thanks.


2 Answers

You can add it as a 'Save Action' so that whenever you save a file, Eclipse will add final to your variables. This is how:

  • Go to Window > Preferences > Java > Editor > Save Actions
  • Tick Additional Actions and press Configure
  • In the Code Style tab there is a section on Variable declarations and you can tick Use modifier 'final' where possible. You can choose between private fields, parameters and local variables.

Or you can do the same thing manually (every once in while or for existing code base) via Clean up

like image 135
dogbane Avatar answered Sep 28 '22 09:09

dogbane


You can use the right click --> source --> clean-up. You should first define in the Code Formatter part of the preference what you really want to be cleaned.

like image 39
YMomb Avatar answered Sep 28 '22 09:09

YMomb