Despite the arguments and debates about getters and setters in Java, the fact is that they’re a reality and you have to work with them.
But managing getters and setters is a time-consuming effort. Creating a getter/setter for 5 fields in a class can take minutes, renaming one is error-prone and deleting one is just plain inconvenient.
There are options like Project Lombok (that implicitly creates getters/setters without the need to code them) and you could avoid getters/setters altogether by redesigning your classes.
But these options aren’t always available, so it’s a good thing Eclipse has some handy features for managing getters and setters. Combined with the ability to generate constructors based on fields, you can get the boilerplate code out of the way in seconds and get on with the real coding.
Generate getters and setters
To generate getters and setters, do the following:
- Create the fields you want in the class then press Alt+Shift+S, R. A dialog will pop up allowing you to choose the fields you want to generate getters and setters for.
- Click Select All to create getters/setters for all fields. Of course you can choose individual fields as required.
- Change Insertion point to Last Member. This tells Eclipse that you want to put the methods at the bottom of the class. This is normally the best option for me as I want them out of the way.
- Click Ok. Eclipse will create the getters and setters for you.
Here’s an example of what the dialog should look like.
Note: By default, Eclipse doesn’t allow you to create a setter for a final field – the setter just doesn’t appear in the dialog. This can be a nuisance, especially if you’ve enabled autoformatting to make fields final where possible. To bypass this restriction, enable the checkbox Allow setters for final fields on the dialog. The setter for the field will now appear in the dialog. Once you click Ok, Eclipse will remove the final keyword from the field and generate the setter. Eclipse also remembers this setting.
Another way to add just a single getter/setter is to position your cursor anywhere in the class (outside any method), start typing either “get” or “set” and press Ctrl+Space. The options on the autocomplete menu will include any getters/setters of fields that don’t have any defined yet. This is a quick way to create a single getter/setter but isn’t geared for bulk creation.
Here’s an example of how the autocomplete looks:
Rename getters and setters
The easiest way to rename getters/setters is to use the Rename refactoring.
Place your cursor on the field name (anywhere in the class, not just the declaration) and press Alt+Shift+R. If you’re using in-place rename (the default), then just rename the field, press Enter and Eclipse will rename the corresponding getters and setters as well.
If you’ve chosen to use the classic Refactor dialog (see note below) then make sure you enable Rename getter and Rename setter on the Rename dialog.
Note: You can choose to do renaming using the traditional Rename dialog by going to Window > Preferences > Java and unchecking Rename in editor without dialog. I prefer using the Rename dialog as it highlights the whole name by default making it easier to overwrite and I have the option of not renaming the getters and setters if I don’t want to. The Eclipse default these days is to use the new in-place renaming.
Although Eclipse will rename the getter/setter, it won’t rename the argument passed to the setter method. If you want consistency, you can navigate to that method (eg. using Ctrl+O) and rename the argument yourself.
Delete getters and setters
Deleting getters and setters isn’t as straightforward as just deleting the field in the editor. However, you can delete a field and its getters/setters from the Outline view.
Open the Outline view (Alt+Shift+Q, O), select the field you want to delete and press Delete (or right-click, Delete). Eclipse will ask you whether you want to delete the getters/setters as well. Just choose Yes To All and they will be removed.
You can select multiple fields simultaneously. And you can delete individual getters/setters (excluding the field) by just selecting the getter/setter and pressing Delete.
- Generate class constructors in Eclipse based on fields or superclass constructors
- Add, remove and reorder a method’s arguments instantly across multiple classes in Eclipse
- Jump to the start and end of methods, loops, blocks and XML tags
- The fastest ways to navigate views in Eclipse using the keyboard
- Convert nested/anonymous classes into top level classes using Eclipse refactoring