IntelliJ IDEA 2023.1 Help

Replace constructor with builder

The Replace Constructor with Builder refactoring helps hide a constructor, replacing its usages with the references to a newly generated builder class, or to an existing builder class.

  1. Place the caret at the constructor invocation in the editor and press Alt+Enter.

  2. From the list of available context actions, select Replace constructor with builder.

    Replace constructor with builder
  3. In the dialog that opens, if you need, change the suggested setter names. Specify whether you want to initialize the generated fields in the builder.

    If you specify an initial value that matches the parameter value in the constructor invocation, you can skip setter method for such parameter by selecting the Optional Setter checkbox. You can also specify whether you want to create a new or use the existing builder.

  4. Preview and apply the changes.

Example

class Foo { Foo(String firstName, String lastName, int age, boolean married) {} public static void main(String[] args) { Foo joe = new Foo("Joe", "Smith", 42, false); } }
class Foo { Foo(String firstName, String lastName, int age, boolean married) { } public static void main(String[] args) { //married is omitted, default "false" is used Foo joe = new FooBuilder() .setFirstName("Joe") .setLastName("Smith") .setAge(42) .createFoo(); } static class FooBuilder { private String firstName; private String firstName; private int age; private boolean married; public FooBuilder setFirstName(String firstName) { this.firstName = firstName; return this; } public Foo createFoo() { return new Foo(firstName, lastName, age, married); } } }

Replace constructor with builder dialog

Use this dialog to specify options for the Replace constructor with builder refactoring.

Item

Description

Parameters to pass to the builder

Parameter

This column shows the list of parameters detected in the constructor, which will be replaced with the builder fields.

Field name

This editable column shows the list of suggested field names in the builder.

Setter name

This editable column shows the list of suggested setter names in the builder.

Default value

Use this editable column to initialize the fields with the default values.

Optional setter

If the specified default value of a field matches the parameter value in the constructor invocation, then selecting this checkbox results in omitting setter method for this field in the builder invocation. If this checkbox is not selected, the corresponding setter method will be shown anyway.

Builder name and location

Create new

Click this radio button to generate a new builder class, with the specified name and destination package.

Builder class name

This editable field shows the suggested name of the new builder class to ge generated. You can accept default, or type a new one.

Package for new builder

Type the name of the destination package, or click the browse button, and locate it in the Choose Destination Package dialog box. If the desired package doesn't exist, click App actions new folder to create a new one.

Use existing

Click this radio button to specify an existing builder class.

Builder class name

Type here the fully qualified name of the desired builder class that already exists in your project, or click the browse button and find it either by name, or in the project tree view.

Last modified: 20 September 2022