Introduction to Java Getter Setter
Getters and Setters in java are two methods used for fetching and updating the value of a variable. Getter methods are concerned with fetching the updated value of a variable, while a setter method is used to set or update an existing variable’s value. Getter and setter methods in java are also named as accessor and mutator, respectively.
Syntax of Java Getter Setter
Below is the syntax of using a getter and setter method in java:
Syntax:
package <packagename>;
public class <className>{
// class member variable
private <datatype> <variableName>;
// getter method to fetch data
public <datatype> get<VariableName>(){
return <variableName>;
}
// setter method to update data
public void set<VariableName>(<datatype> variableName){
this.<variableName>= variableName;
}
}
In the above syntax following symbols are used:
- <packagename>: Name of the package.
- <className>: Name of the class.
- <variableName>: Name of the variable.
- <datatype>: Data Type of the variable.
The main intention of using getter and setter in java is to provide data hiding. Variables of a class are marked as private, and getters & setters are exposed in order to fetch and update the value of that variable. With the use of getters and setters, a developer can control how variables can be accessed and updated in a proper condition. Therefore, getters and setters provide access security to a variable that protects its value from being changed from the outside world. Let us consider an example to make things clear:
Example:
public class Test {
//String variable marked as private
private String name;
// getter method
public String getName() {
return name;
}
// setter method
public void setName(String name) {
this.name = name;
}
}
In the above example, the variable name is marked as private and getters; setters are exposed. Marking variable as private provides data hiding. Changes to variable name cannot be made from the outside world.
The below-naming convention is followed for naming getter and setter methods:
- get<variableName>
- set<variableName>
Methods of Getter & Setter
The below table shows the getter and setter method for the given variable name.
Variable Name | Getter Method | Setter Method |
private int data | public int getData() { return data; } |
public void setData(int data) { this.data = data; } |
private ArrayList elements; | public ArrayList getElements() { return elements; } |
public void setElements(ArrayList elements) { this.elements = elements; } |
private String name; | public String getName() { return name; } |
public void setName(String name) { this.name = name; } |
private Date date; | public Date getDate() { return date; } |
public void setDate(Date date) { this.date = date; } |
private boolean flag; | // in case of boolean get is replaced with ‘is’ public boolean isFlag() { return flag; } |
public void setFlag(boolean flag) { this.flag = flag; } |
Example for Java Getter Setter
This example shows how to declare and use the getter and setter method for a variable having different data types.
Code:
package com.edubca.gettersetterdemo;
import java.util.ArrayList;
import java.util.Date;
import java.util.function.Consumer;
public class Test {
private int number;
public ArrayList languages;
private String name;
private Date date;
private boolean flag;
public int getNumber() {
return number;
}
public void setNumber(int data) {
this.number = data;
}
public ArrayList getLanguages() {
return languages;
}
public void setLanguages(ArrayList elements) {
this.languages = elements;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public static void main(String[] args) {
Test test =new Test(); // Creating an instance of class
test.setNumber(10); // setting value of Integer Data
int output= test.getNumber(); // reading value of Integer Data
System.out.println("Value of Integer Data is :" + output);
System.out.println();
test.setName("Edubca"); //setting value of String variable
String stringOutput= test.getName(); // reading value of String variable
System.out.println("Value of String Data is :" + stringOutput);
System.out.println();
ArrayList list =new ArrayList();
list.add("Java");
list.add("Hadoop");
list.add("Spark");
list.add("Hive");
list.add("Python");
list.add("C++");
list.add("Machine Learning");
test.setLanguages(list); // set list of languages
ArrayList languages=test.getLanguages(); // get list of languages
System.out.println("List of languages is : ");
languages.forEach(new Consumer<String>() {
@Override
public void accept(String t) {
// TODO Auto-generated method stub
System.out.println(t);
}
});
System.out.println();
Date date =new Date();
test.setDate(date); // setting value of date variable
Date outdate=test.getDate(); // reading value of date variable
System.out.println("Current Date is : " + outdate);
System.out.println();
test.setFlag(true); // set boolean variable
boolean outflag=test.isFlag(); // reading boolean variable
System.out.println("Flag is :" + outdate);
}
}
Output:
Conclusion
The above article provides a clear understanding of the use of getters and setters in java. Using getter and setter is considered as best practice in java development. We use getters and setters in our application as they help create secure, roust and maintainable code.
Recommended Article
This is a guide to Java Getter Setter. Here we discuss the use of getters and setters in java and its Methods and Example and code implementation. You can also go through our other suggested articles to learn more –