Getters and Setters in java
Getters and setters in Java class programming are system-defined class methods. The getter and setter concepts in Java are used to access the values of private instance parameter variable fields of a class and to manually customize or modify them. Getter and setter methods in Java follow the encapsulation process, which is one of the object-oriented programming (OOP) concepts in Java programming. The Java OOP concept of encapsulation is used to hide the internal Behavior of a class object and provide controlled access to it through getters and setters.

What are getters and setters in Java?
A getter is a class method in Java class programming, also known as a class element variable data type accessor method in Java. Getter methods in a class are used to retrieve or retrieve the value of a private instance variable of a data type in a user-defined class.
Setter methods are a popular method in Java classes, also known as mutator methods. Setter methods are used to set or modify the value of a private instance variable of a data type in a user-defined class.
Getter and setter methods in Java provide direct, controlled access to private variables defined in a class. This allows Java user classes to apply rules or validation logic when retrieving or setting the value of data variables.
Why use getters and setters in Java classes?
- Encapsulation – In Java, OOPs concepts help control or manage the encapsulation of a class element, allowing Java users to access or modify class instance variables when needed.
- Data validation – Setter methods in Java classes help validate or transform class data before assigning a value to an instance variable.
- Read-only or write-only access – Getter and setter methods in Java classes can be used to provide read-only or write-only access to a user-declared class variable.
Java getters/accessors are methods within a class.
Geter methods in user-defined classes are used to retrieve the value of a private instance variable. This generally follows the naming convention in Java: get, followed by the class variable name in capital letters.
Example of a getter method in a Java class.
public class Employee {
private String emp_name; // Here we declare the employee’s private instance variable
// Here we define a getter method for ’emp_name’
public String getemp_name() {
return emp_name; // Here it returns the value of the ’emp_name’ employee class variable
}
}
Getter method in a Java class example.
- Here getemp_name is a getter class method that extracts the value of the emp_name instance variable.
Setters/Mutators in Java.
Setter methods in Java classes are used to set or modify the value of private instance class parameter variables. In Java, setter method naming conventions generally follow the set concept, so that the name of the variable used later is displayed in capital letters.
Example of setters/mutators.
public class Employee {
private String emp_name; // Here we define the Employee class’s private instance variable emp_name
// Here we define a setter method for the ‘ emp_name ‘ variable
public void set emp_name(String emp_name) {
this. emp_name = emp_name; // Here we set the value of the ‘ emp_name ‘ class variable
}
}
Setters/Mutators in Java Example.
- Here in the Employee class, set emp_name(String emp_name) is a user-defined class setter method that assigns a new value to the emp_name instance variable.
Complete example of getters and setters in a Java class.
So, let’s now understand the class methods in an Employee class, where we have both getter and setter methods for all instance variables.
public class Employee {
private String emp_name; // here employee class Private emp_name instance variable defined
private String age; //here employee class Private age instance variable
private int salary; //here employee class Private age instance variable
// here we define Getter for ’emp_name’
public String getEmp_name() {
return emp_name;
}
// here we define Setter for ’emp_name’
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
// here we define Getter for ‘age’
public String getAge() {
return age;
}
// here we define Setter for ‘age’
public void setAge(String age) {
this.age = age;
}
// here we define Getter for ‘salary’
public int getSalary() {
return salary;
}
// here we define Setter for ‘salary’
public void setSalary(int salary) {
if (salary > 21000) { // here we use Basic validation to ensure valid employee salary
this.salary = salary;
} else {
System.out.println(“Invalid employee salary!.”);
}
}
//here we use Method to display employee class details
public void empInfo() {
System.out.println(“Employee emp_name is – ” + emp_name);
System.out.println(“Employee age is – ” + age);
System.out.println(“Employee salary is – ” + salary);
}
}
public class Main {
public static void main(String[] args) {
// here we Create a new Employee class object
Employee testemployee = new Employee();
// here we Setting values for employee class object using setters method
testemployee.setEmp_name(“Bhavishi”);
testemployee.setAge(“21”);
testemployee.setSalary(9999);
//Getting values using getters
System.out.println(“Employee emp_name is – ” + testemployee.getEmp_name());
System.out.println(“Employee age is – ” + testemployee.getAge());
System.out.println(“Employee salary is – ” + testemployee.getSalary());
// here it Display employee class instance object information
testemployee.empInfo();
}
}
Explaining getter and setter methods in Java.
- Here, the instance variables emp_name, age, and salary are defined as private in the Employee class. The getter methods getEmp_name(), getAge(), and getSalary() in the Employee class extract the values of their respective variables.
- Similarly, the setter methods setEmp_name(), setAge(), and setSalary() in the Employee class are used to modify the values of instance variables.
- Similarly, the setter validation method setSalary() in the setter method provides simple validation to ensure that the employee’s salary is greater than 21,000.
In the main Employee class.
- We create an object testemployee in the Employee class and use the setter method to set the values of the Employee class attributes.
- Similarly, we use the getter method to extract and display the attributes of the Employee class.
- Finally, we display the Employee class information using the empInfo() method.

