How To Add Fields And Generating Getter-Setters
10 April 2007Normally it is a good practice to have class variables declared as private and use non private “getter and setter” methods to access them. This is otherwise called as “Data Encapsulation” in Object Oriented Programming.
NetBeans provides a way to automatically generate getters and setters of your class variables. It is fast and efficient. Here are the steps.
- In the “Projects” View, Expand the class and then “Bean Patterns”.
- In next window specify information as per your choice.
- Click “OK” code will be generated In IDE.
Normally it is a good practice to have class variables declared as private and use non private “getter and setter” methods to access them. This is otherwise called as "Data Encapsulation" in Object Oriented Programming. For those who are still confused about getters and setters, go through this code: package javatips; public class Car { /** Creates a new instance of Car */ public Car() { } /** * Holds value of property color. */ private String color; /** * Getter for property color. * @return Value of property color. */ public String getColor() { return this.color; } /** * Setter for property color. * @param color New value of property color. */ public void setColor(String color) { this.color = color; } }
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: How To Add Fields And Generating Getter-Setters