Getters and Setters in Java

Getter and Setter are two conventional methods that are used for retrieving and updating value of a variable.By using getter and setter, the programmer can control how his important variables are accessed and updated in a correct manner, such as changing value of a variable within a specified range.

Providing getter and setter method for accessing any field of class in Java may look unnecessary  because you can make field public and it’s accessible from everywhere in Java program. In fact many programmers do this in there early days but once you start thinking in terms of enterprise application or production code, you will start seeing how much trouble it can create in terms of maintenance.

Getter and setter method gives you centralized control on how a particular field is initialized and provided to client which makes validation and debugging much easier. you can simply put a breakpoints or print statement to see which thread are accessing and what values are going out.

Rules for get/set:

  • The getter method’s prefix must be get, if the property is not of boolean type
  • The getter method’s prefix can be get or is, if the property is of boolean type
  • The setter method’s prefix must be set
  • The signature of the setter method must be public with void as return type, and an argument that represents the property type
  • The signature of the getter method must be public with no argument, and a return type matching the setter method of that property

Example:1

int num;
boolean visible;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}

Example:2

class Abc {
private int a;
public int b;
public int getA() {
return a;
   }
public void setA(int x) {
a = x > 100 ? 100 : x < 0 ? 0 : x;
   }
}
Abc obj = new Abc();
obj.setA(10000);
obj.b = 10000;

Example :3

class MyClass {
private int a;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
MyClass obj1 = new MyClass();
obj1.setA(100);
//obj1 is the current context. Current context is the object in use.
MyClass obj2 = new MyClass();
obj2.setA(200);
//obj2 is the current context. Current context is the object in use.

In Java, current context in class is referred usingthis“. “this” becomes compulsory when member data and arguments shares a common name.

Example:4

class Employee {
String name;
String dept;
String desig;
public void setData(String a, String b, String c) {
name = a;
dept = b;
desig = c;
}
public void setData(String name, String dept, String desig) {
this.name = name;
this.dept = dept;
this.desig = desig;
}

public V/S get/set:

  • with get/set we have options of read or write or both. But, public data always have both the options
  • It is possible to validate the data with set. But, public data cannot be validated

Constructors :

Constructors are special member functions used for initialization of user define data types in multiple forms.

  • Same name as of a class
  • Does not have any return type, not even void
  • Executed automatically whenever an object is created (new)
  • Executed only once for an object

 Constructor Rules :

  • If we do not write a constructor, Java adds a default constructors that initializes all numeric member data is by default initialized by 0, boolean by false, charcter by space and reference type by null
  • Number of constructors in class = Number of ways objects can be created

 

For Complete programs of Getter & Setter and its Implementations visit P3lang GitHub Repo

%d bloggers like this: