IntelliJ IDEA 2021.3 Help

Encapsulate Fields dialog

Refactor | Encapsulate Fields

Item

Description

Fields to encapsulate

In this area select the fields you want to create accessors for. You can accept the default method names or change them at will. If a method with the same signature is already present in the class to be refactored, the Method icon Method appears to the left of the accessor name, along with the corresponding visibility icon. No new accessor method will be created in this case. The existing method will not be changed and, generally, nothing else will be additionally checked. In this case you have to be attentive whether the existing method actually functions as an accessor. Otherwise, you might need either to choose another name for the accessor or to go back to the editor, change the existing method and only then invoke the Encapsulate Fields refactoring again. If the existing method overrides/implements a parent class' method it will also be marked with the Overrides method Gutter icon overriding or Implements method Gutter icon implements icon. If the accessor method does not exist in the class to be refactored but, if created it would override/implement a method of the parent class such method will be marked with the Overrides method Gutter icon overriding or Implements method Gutter icon implements icon only to the left of the accessor name. A new accessor method will be generated. However, this newly created accessor method will actually override or implement the method of the parent class, that might not exactly be what you planned it for. In this case, you might need either to choose another name for the accessor or to go back to the editor, change the existing method and only then invoke the Encapsulate Fields refactoring again.

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 option Use accessors even when the field is accessible is not checked, 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: 01 August 2022