Auto generating getters and setters

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:

public class Sample{
	private String stringtext;public String getStringtext() {
 
		return stringtext;
 
	}
 
	public void setStringtext(String stringtext) {
 
		this.stringtext = stringtext;
 
	}
 
}

Eclipse provides an way to automatically generate getters and setters of your class variables. Its fast and efficient. Go through the attached 2 screen shots.

Eclipse screen 2

The final code would look like this.

public class MyClass{
	private int length, width, area;
 
	public int getLength() {
		return length;
 
	}
 
	public void setLength(int length) {
 
		this.length = length;
 
	}
 
	public int getWidth() {
 
		return width;
 
	}
 
	public void setWidth(int width) {
 
		this.width = width;
 
	}
 
	public int getArea() {
 
		return area;
 
	}
 
	public void setArea(int area) {
 
		this.area = area;
 
	}
 
}