llms.txt: machine-readable documentation index. Markdown version of this page is also available.

IntelliJ IDEA 2026.3 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.

Example

The following example shows how encapsulating a field changes the class that declares it and a class that references it.

// Car.java public class Car { public String color; } // Garage.java public class Garage { private Car car; public void repaint(String newColor) { car.color = newColor; } }
// Car.java public class Car { private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } } // Garage.java public class Garage { private Car car; public void repaint(String newColor) { car.setColor(newColor); } }

Encapsulate Fields dialog

Use the Encapsulate Fields dialog to select which fields to encapsulate, which accessors to create for them, and which access modifiers to use.

Item

Description

Fields to Encapsulate

Select the fields for which you want to create accessors. The Getter and Setter columns show suggested accessor names, which you can edit.

Get access

Set access

Select which accessors to create for the selected fields: getters, setters, or both.

Use accessors even when field is accessible

Replace all references to the selected fields with accessor calls, even when the references can access the fields directly (for example, inside the same class).

If you clear this option, references that can still access a field directly after the refactoring will keep this access. For example, if you set Encapsulated Fields' Visibility to Private, references inside the class will keep accessing the field directly, and only the references outside the class will change to accessor calls.

Javadoc

This option appears only if at least one field has a Javadoc comment.

Select what to do with Javadoc comments for the selected fields:

  • As is: keep the comment on the field only.

  • Copy: copy the comment to the getter and keep it on the field.

  • Move: move the comment from the field to the getter.

Encapsulated Fields' Visibility

Select the access modifier for the selected fields:

  • Private: make the fields private.

  • Package local: remove existing access modifiers so that the fields become package-private.

  • Protected: make the fields protected.

  • As is: keep the existing access modifiers. With this option, IntelliJ IDEA will replace all references to the fields with accessor calls.

Accessors' Visibility

Select the access modifier for the new accessors:

  • Public: make the accessors public.

  • Protected: make the accessors protected.

  • Package local: create the accessors without an access modifier so that they are package-private.

  • Private: make the accessors private.

20 November 2026