IntelliJ IDEA 2024.2 Help

Encapsulate Fields

The Encapsulate Fields refactoring lets you hide your data and create the necessary accessors.

Hiding your data and accessing it through an outward interface based on accessor methods is a good idea. Later you can change the data internals, preserving full compatibility with the code relied on the class and its available methods.

  1. In the editor, place the caret at a class or at the desired field or at any place inside the class you want to refactor. (You can also use the Project tool window or Structure View for your selection.)

  2. From the main menu or from the context menu, select Refactor | Encapsulate Fields.

  3. In the dialog that opens, check the fields to which you want to create accessors, specify whether you want to create getter or setter methods. Plus, select the Use accessor even when field is accessible if you want to replace all field occurrences with the calls to the appropriate accessor method. You can also select visibility options.

  4. Preview and apply changes.

Examples

Before

After

//File Class.java public class Class { public String aString; }
//File Class.java public class Class { private String aString; public void setaString(String aString) { this.aString = aString; } public String getaString() { return aString; } }
//File AnotherClass.java public class AnotherClass { public Class aClass; public void method() { aClass.aString="string"; } }
//File AnotherClass.java public class AnotherClass { public Class aClass; public void method() { aClass.setaString("string"); } }

Encapsulate fields dialog

Use this dialog to specify options for the Encapsulate fields refactoring.

Item

Description

Fields to Encapsulate

In this area, select the fields to which you want to create accessors. You can keep the default names or rename them.

If a method with the same signature already exists in the class you're refactoring, the Method icon (the Method icon) will appear and no new accessor method will be created.

You should check if the existing method works as an accessor. If it doesn't, you may need to rename the accessor or change the existing method before using the Encapsulate Fields refactoring again. If the new accessor overrides (the Overriding method icon) or implements (the Implemented method icon) a method of the parent class, it will be marked accordingly. You may need to choose a different name or change the existing method in this case as well.

Get access/Set access

Use this option group to select which accessor methods (Getter, Setter or both) will be created for the selected fields. If one of the checkboxes is cleared, the entire corresponding column (Getter or Setter) in the Fields to Encapsulate table is disabled.

Encapsulated Fields' Visibility

Here you can specify the new visibility scope for the selected fields

Options

Select whether you want to use accessors even when field is accessible or not. If the Use accessors even when the field is accessible option is not enabled, the references to the desired fields, when the fields are directly accessible, will not change. Otherwise, all references to the desired fields will be replaced with the accessor calls. It also depends on your selection in the options group Encapsulated Fields' Visibility. For example, if you uncheck the option Use accessors even when the field is accessible, and select the private visibility for the fields, the usages of the fields outside the class will change, but within the class they will remain the same.

Accessors' Visibility

In this area select the visibility scope for the created accessor methods.

Last modified: 11 February 2024