Generate class constructors in Eclipse based on fields or superclass constructors

You’ll often need to add a constructor to a class based on some/all of its fields or even based on constructors of its superclass. Take the following code:

public class Contact {
    private String name, surname;
    private int age;

    public Contact(String name, String surname, int age) {
        this.name = name;
        this.surname = surname;
        this.age = age;
    }
}

That’s 5 lines of code (lines 5-9) just to have a constructor. You could write them all by hand, but writing a constructor that accepts and initialises each field takes a lot of time and becomes irritating after a while. And creating constructors from a superclass can take even longer because the superclass can define multiple constructors that you need to reimplement.

That is why Eclipse has two features to help you generate these constructor instantly: Generate Constructor using Field and Generate Constructor from Superclass. Both features will generate a constructor in seconds, freeing you up to get to the exciting code. You’ll also see how to add/remove/reorder arguments of an existing constructor based on fields defined in the class.

Generate a constructor from fields

The fastest way to generate a constructor based on fields is to press Alt+Shift+S, O (alternatively select Source > Generate Constructor using Fields… from the application menu). This pops up a dialog where you can select the fields you want to include in the constructor arguments. Once you’ve selected the fields you want, just click Ok and you’re done. BTW, Alt+Shif+S is the shortcut to display a shortened Source menu, allowing Java source editing commands.

The following video shows an example of how much time this feature can save you. We’ll create a constructor for the class Message.

Notes:

  • You can additionally call a superclass constructor with a subset of the fields by changing the dropdown Select super constructor to invoke at the top of the dialog. This creates a super(…) call with the relevant arguments and initialising code for the rest of the arguments on your subclass’s constructor.
  • If you don’t want the JavaDoc for the constructor, disable the checkbox Generate constructor comments on the dialog.
  • You can include/exclude the calls to super() using the checkbox Omit call to default constructor super(), on the dialog.
  • You have to be positioned in a class to invoke this command.

If you use frequently use this command, you can remap its keyboard shortcut by changing the key binding for the command Generate Getters and Setters.

Generate constructor(s) from a superclass

Sometimes you’ll want to reimplement some/all of a superclass’s constructors, especially as part of the contract.

To generate constructor(s) from a superclass, just press Alt+Shift+S, C (or alternatively select Source > Generate Constructor from Superclass… from the application menu). A dialog pops up allowing you to select the constructor(s) you’d like to create. Once you click Ok, Eclipse generates the constructor, together with a super() call.

Here’s an example of how to create a constructor in SecretMessage, that inherits from the class Message. Message has three constructors: a default one, one that accepts one String (content) and another that accepts three Strings (content, fromAddress and toAddress). SecretMessage should only expose the last two constructors.

Note: You have to be positioned in a class to invoke this command.

If you use this command frequently, you can remap its keyboard shortcut by changing the key binding for the command Generate constructors from superclass.

Add, reorder and remove fields on existing constructors

If you have an existing constructor and want to reorder its arguments or remove some of them, have a look at the Change Method Signature refactoring that does that in a jiffy.

If you want to add a single field to an existing constructor, have a look at the next video that uses Eclipse’s Quick Fix (Ctrl+1) to do that easily. I’ll add a field createdDate to an existing constructor in Message by choosing Assign parameter to field from the Quick Fix menu while positioned on the field.

Related Tips

Now that creating constructors is as easy as making lunch, have a look at some other tips that help you generate boilerplate code faster.

Share This Tip

Support Eclipse On E

Comments are closed.