This tutorial explains how to add setters and getters for a variable in the eclipse code editor.

Generate Setter and Getter for a variable of a class in Eclipse.

Following are steps to generate set and get methods of a class.

For example, the Java code editor is opened with Employee.java class

class Employee{
    private id Integer;

}
  • Next, Right click on the cursor located inside a class, It shows the context menu.
  • Select Source Context Menu Item
  • It popups sub-menu and select Generate Getters and Setters
  • It opens the window and selects the variables that you want to generate
  • It generates the following things
class Employee{
    private id Integer;

    public void setId(Integer id){
        this.id=id;
    }
    public Integer getId(){
        return id;
    }

}

Shortcut key to generate Setter and getter in eclipse

In Windows, You can use the below command for both setter and getter

  • Alt + Shift + S, Then press R
  • Next, Select single or all variables to generate it

Another way

  • Alt + Shift + S, Then press R
  • (Or) Then press l, click on the Ok button to generate only Setter
  • (Or) Then press g, click on the Ok button to generate only Getter

Eclipse setter and getter assign for key binding

Following are steps for assigning hotkeys to Setter and Getter

  • Open Windows -> Preferences , Opens window
  • Select General tab -> Keys
  • Select Command as Generate Getters and Setters
  • In the binding value, Give CTRL +SHIFT +T as a new binding
  • Finally, Apply the button to save changes.