Generating Getter and Setter Methods
See Also
You can generate getter and setter methods for a field in one of the following two ways:
- By using the code completion box.
This way is easier if you have just created the
field in your code and want to add the getter and setter methods.
- By using the Encapsulate Field refactoring command.
This way is generally preferable if you
also want to change the field's access modifier and change other code to use the
getter and setter methods to access the field.
To generate getter and setter methods using the code completion box:
- Type the field declaration in the Source Editor and press Enter.
- Press Ctrl-Spacebar.
- In the code completion box, scroll down to the entry
Create getter getFieldName for field FieldName and press Enter.
- Press Ctrl-Spacebar.
- In the code completion box, scroll down to the entry
Create setter setFieldName for field FieldName and press Enter.
To generate getter and setter methods using the Encapsulate Fields command:
- Right-click any nonstatic field in the Projects window or Source Editor
and choose Refactor > Encapsulate
Fields.
- In the Encapsulate Fields dialog box, set the visibility for the field and the accessors
and click Next.
- In the Output window, click Do Refactoring.
Getter and setter methods are generated for that field if they do not
already exist. Because generating getter and setter methods for a field makes
that field a bean property, a node for the field also appears under the
Bean Patterns node.
- See Also
- Creating a Bean
- Creating a Bean Property
Legal Notices