Generate, rename and delete getters/setters instantly in Eclipse

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:

  1. 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.
  2. Click Select All to create getters/setters for all fields. Of course you can choose individual fields as required.
  3. 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.
  4. 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 need to have fields visible in the Outline view to use this feature (ie. untoggle the Hide Fields button ).

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.

Related Tips

Support Eclipse On E

5 thoughts on “Generate, rename and delete getters/setters instantly in Eclipse

    • Good point, Eko, but I think there’s still some debate about whether you actually want getters/setters or introduce properties. I think that Java would benefit from some form of properties. See this link for more on the property proposal that almost made it into Java 7.

      Also, I don’t like being forced to create getters/setters just so that I can use a library eg. Spring/Hibernate, etc.

      But having the IDE’s around does make it a lot simpler, so it’s something you get used to, I suppose.

  1. Pingback: weekly links 2010-42 & 43 « /home/edivad

  2. Do you know if and how it is possible to customize the generated getter/setter methods?
    For example:

    public void setFoo(String newFoo) {
    foo = newFoo;
    }

    or

    public void setFoo(String newFoo) {
    final String oldFoo = foo;
    foo = newFoo;
    firePropertChange(“foo”, oldFoo, newFoo);
    }

    • I don’t know how to get exactly what you’re looking for and I don’t even think Eclipse supports it but you could try some of the following.

      You could generate the default setter and then use normal Eclipse templates to generate notifications based on the field name. But this isn’t perfect though. Your template will have to prompt for the uppercase version of the field as well to get the camelcase (eg. newFoo). And you’ll need to add/remove the this. prefix depending on your Eclipse settings.

      The template would look something like this. Play around and see if it can work for you:

      final ${type} old${upperName} = ${theField:field};
      ${theField} = ${enclosing_method_arguments};
      firePropertyChange("${theField}", old${upperName}, ${enclosing_method_arguments});

      Another approach could be to change the setter body template under the Code section found in Window > Preferences > Java > Code Style > Code Templates. However, this applies to ALL setters, including classes where you don’t want notifications to fire. And it’s not flexible enough to do what you want, eg. the field name can’t be capitalised in certain places only (eg. newFoo) and if you’ve configured Eclipse to add the prefix this to your field, then fieldname becomes this.foo everywhere.

      If you feel enthusiastic, you could write your own template variable resolver in a plugin or maybe see if there’s a plugin that does that already.