- Opening the Source Editor
- Managing Automatic Insertion of Closing Characters
- Displaying Line Numbers
- Generating Code Snippets without Leaving the Keyboard
- Using Code Completion
- Inserting Snippets from Code Templates
- Using Editor Hints to Generate Missing Code
- Matching Other Words in a File
- Generating Methods to Implement and Override
- Generating JavaBeans Component Code
- Creating and Using Macros
- Creating and Customizing File Templates
- Handling Imports
- Displaying Javadoc Documentation While Editing
- Formatting Code
- Text Selection Shortcuts
- Navigating within the Current Java File
- Navigating from the Source Editor
- Searching and Replacing
- Deleting Code Safely
- Changing a Method's Signature
- Encapsulating a Field
- Moving a Class to a Different Package
- Moving Class Members to Other Classes
- Creating a Method from Existing Statements
- Creating an Interface from Existing Methods
- Extracting a Superclass to Consolidate Common Methods
- Changing References to Use a Supertype
- Unnesting Classes
- Tracking Notes to Yourself in Your Code
- Comparing Differences Between Two Files
- Splitting the Source Editor
- Maximizing Space for the Source Editor
- Changing Source Editor Keyboard Shortcuts
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:
- Right-click the field or the whole class in the Source Editor or the Projects window and choose Refactor | Encapsulate Fields.
-
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.
-
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.
- In the Accessors' Visibility drop-down list, set the access modifier to use for the generated getters and setters.
-
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.
-
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.
-
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.