Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Encapsulating a Field

One common design pattern in Java programs is to make fields accessible and changeable only by methods in the defining class. In the convention used by JavaBeans components, the field is given private access and accessor methods are written for the field with broader access privileges. The names of the accessor methods are created by prefixing get and set to the field's name.

If you have fields that are visible to other classes and would like to better control access to those fields, you can use the IDE's Encapsulate Fields command to automate the necessary code modifications. The Encapsulate Fields command does the following things:

  • Generates getter and setter methods for the desired fields.
  • Enables you to change the access modifier for the fields and accessor methods.
  • Changes code elsewhere in your project that accesses the fields directly to instead use the newly generated accessor methods.

To encapsulate fields in a class:

  1. Right-click the field or the whole class in the Source Editor or the Projects window and choose Refactor | Encapsulate Fields.
  2. In the Encapsulate Fields dialog box, select the Create Getter and Create Setter checkboxes for each field that you want to have encapsulated.

    If you have selected a specific field, the checkboxes for just that field should be selected by default.

    If you have selected the whole class, the checkboxes for all of the class' fields should be selected by default.

  3. In the Fields' Visibility drop-down list, set the access modifier to use for the fields that you are encapsulating.

    Typically, you would select private here. If you select a different visibility level, other classes will still have direct access to the fields for which you are generating accessor methods.

  4. In the Accessors' Visibility drop-down list, set the access modifier to use for the generated getters and setters.
  5. If you decide to leave the fields visible to other classes but you want to have current references to the field replaced with references to the accessor methods, select the Use Accessors Even When Field Is Accessible checkbox. Otherwise, those direct references to the field will remain in the code.

    This checkbox is only relevant if you decide to leave the fields accessible to other classes and there is code in those classes that accesses the fields directly.

  6. Click Next.

    If you have deselected the Preview All Changes checkbox, the changes are applied immediately.

    If you leave the Preview All Changes checkbox selected, the Refactoring window appears with a preview of the changes.

  7. In the Refactoring window, verify the changes that are about to be made and click Do Refactoring.

    If you later find that the refactoring has had some consequences that you would like to reverse, you can choose Refactor | Undo.

  • + Share This
  • 🔖 Save To Your Account