-2
public class Employee {

    private long id;
    private String firstname;
    private String lastname;
    private String emailId;
    
    

}

enter image description here

Jens
  • 67,715
  • 15
  • 98
  • 113
  • that image really doesn't add anything. What do you mean: "generate getters and setters"? You mean by using a tool like lombok, or having your ide generate them? – Stultuske Mar 14 '22 at 10:27
  • You have the answer in this ticket : https://stackoverflow.com/questions/21074402/generate-getters-and-setters-in-netbeans – claudy399 Mar 14 '22 at 10:31
  • You should find the solution in the netbeans help – Jens Mar 14 '22 at 10:32
  • I've tried using the alt + ins shortcut but nothing comes up, Even when i right click there there is no option to generate constructor, getters or setters . This works perfectly on netbeans 8.2 but not in v12, i don't know why – bosso_ivan Mar 14 '22 at 10:42

1 Answers1

1

To generate getters and setters:

  • Place the cursor within the class where your fields have been declared. Position it where you want the generated code to be inserted.
  • Right click. From the context menu that is displayed select Insert Code...
  • The Generate context menu is displayed: generateMenu
  • Select Getter... or Setter... or Getter and Setter... from that context menu as appropriate (though in your case you want Getter and Setter...), and press [enter].
  • The Generate Getters and Setters dialog is displayed: dialog
  • In that dialog check the check box next to the class name, or click Select All, to generate a getter and a setter for all fields. Otherwise check the specific fields for which you want a getter and a setter to be generated. In my case I checked only the fields firstname and lastname, and then clicked Generate.

This is the result:

package employee;
public class Employee {

    private long id;
    private String firstname;
    private String lastname;
    private String emailId;

    public static void main(String[] args) {
        // TODO code application logic here
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

}

Notes:

  • The process has not changed since NetBeans 8.x as far as I know.

  • Keyboard shortcuts can vary according to the environment, so it is more reliable to right click the mouse in order to access the Generate context menu instead. However, since you do not see a menu entry for Getter and Setter... in the Generate context menu, it may be that you have the cursor in an inappropriate place. For example, if I place the cursor at the very end of my Employee.java file I see a suitably reduced context menu without an entry for Getter and Setter...

    reducedMenu

  • If you are still stuck, update your question with a screen shot showing your entire class, and the context menu that is shown after right clicking with the cursor in an appropriate place (such as immediately before the final curly brace of your Employee class).

skomisa
  • 16,436
  • 7
  • 61
  • 102