How To Add Fields And Generating Getter-Setters

10 April 2007

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.

NetBeans provides a way to automatically generate getters and setters of your class variables. It is fast and efficient. Here are the steps.

  1. In the “Projects” View, Expand the class and then “Bean Patterns”.
  2. In next window specify information as per your choice.
  3. 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;
    }
 
}

del.icio.us:How To Add Fields And Generating  Getter-Setters digg:How To Add Fields And Generating  Getter-Setters spurl:How To Add Fields And Generating  Getter-Setters wists:How To Add Fields And Generating  Getter-Setters simpy:How To Add Fields And Generating  Getter-Setters newsvine:How To Add Fields And Generating  Getter-Setters blinklist:How To Add Fields And Generating  Getter-Setters furl:How To Add Fields And Generating  Getter-Setters reddit:How To Add Fields And Generating  Getter-Setters fark:How To Add Fields And Generating  Getter-Setters blogmarks:How To Add Fields And Generating  Getter-Setters Y!:How To Add Fields And Generating  Getter-Setters smarking:How To Add Fields And Generating  Getter-Setters magnolia:How To Add Fields And Generating  Getter-Setters segnalo:How To Add Fields And Generating  Getter-Setters gifttagging:How To Add Fields And Generating  Getter-Setters

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

Leave a Reply