IDEs have become very popular and intelligent to make programming easy for developers. Netbeans is one of the best IDE used for Java programming. In this post, I'm going to explain how to generate setters and getters using Netbeans IDE.

Just start adding a class with instance variables as shown below


public class Person
{
private String name;
private int age;
private boolean graduate;
}

Right click on source code and choose the option Refactor -> Encapsulate Fields... or you can alternatively choose this option from "Refactor" menu

You will get to see below dialog box with the above 3 instance variables. You'll have various different options to generate instance variables. i.e.; you can add java documentation, or you can just add getters alone or you can add setters alone etc.

That's it, once you've chosen the options (I've just clicked "Select All" and clicked on "Refactor") click on Preview or Refactor.

I could see below auto generated code as part of the class

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public boolean isGraduate() {
return graduate;
}

public void setGraduate(boolean graduate) {
this.graduate = graduate;
}