Getter and Setter in Java | Example Program

Getter method in Java

A method which is used to retrieve/get the value of a variable or return the value of the private member variable is called getter method in Java.

This method is also known as accessor method. For every private variable, we should create a getter method.

Depending on the access level giving to the variable, we can set the access modifier of its getter method. If we declare instance variables as private, we will have to add public getter methods for each one.

The signature of a getter method in Java is as follows:

public returnType getPropertyName()

If the returnType is boolean, the getter method should be defined by convention as follows:

public boolean isPropertyName()

Setter method in Java

A method which is used for updating or setting the value of a variable is called setter method in Java. This method is also known as mutator method.

By using the setter method, we can modify the value of a variable. Just like with the getter method, we should create a setter method for every variable in the class.

The signature of setter method in Java is as follows:

public void setPropertyName(dataType propertyValue)

Naming convention of Getter and Setter method

The naming of setter and getter can be done by using the Java bean naming convention. They are as follows:

getXxx() and setXxx() where Xxx is the name of the variable.

For example:

If the variable is the type of int, you will declare like this:

private int number;

then appropriate getter and setter will be like this:

public getNumber()  {  }
public void setNumber(int number)  {  }

If the variable is a type of boolean then

private boolean rich;
public getRich()  {  }
public setRich()  {  }

Let’s take simple example programs to understand getter and setter methods clearly.

Java Getter and Setter method Example Program

Let’s take an example program where we will make a class named a Mensuration. In this class, we will declare private variables and a couple of getter and setter methods.

In this program, we will calculate the circumference, area of the circle, and area, the perimeter of the rectangle using the getter and setter method. Look at the program source code.

Program source code 1:

package gettersetterProgram; 
public class Mensuration 
  public double x, y, r; 
// Create getter method for each variable. 
  public double getX() { 
    return x; 
 public double getY() { 
    return y; 
public double getR() { 
   return r; 
// Create setter method for each variable in the class. 
 public void setX(double x) { 
    this.x = x; 
 public void setY(double y) { 
    this.y = y; 
public void setR(double r) { 
  this.r = r; 
// Now write the logic of Rectangle and Circle. 
  public double areaRec() { 
    double area = x*y; 
    return area; 
 public double perRec() { 
    double per = 2*(x+y); 
     return per; 
 public double areaCircle() { 
    double area = 3.14*r*r; 
     return area; 
 public double circumCircle() { 
    double circumference = 2*3.14*r; 
    return circumference; 
public class MyTest { 
public static void main(String[] args) 
  Mensuration mens; // Creating reference. 
    men's = new Mensuration(); // Creating object.
// Set the values of the variables. 
  double areaRec = mens.areaRec(); 
  double perRec = mens.perRec(); 
  double areaCircle = mens.areaCircle(); 
  double circumCircle = mens.circumCircle(); 

  System.out.println("Area of rectangle: " +areaRec); 
  System.out.println("Perimeter of rectangle: " +perRec); 
  System.out.println("Area of circle: " +areaCircle); 
  System.out.println("Circumference of circle: " +circumCircle); 
       Area of rectangle: 625.25 
       Perimeter of rectangle: 102.0 
       Area of circle: 490.625 
       Circumference of circle: 78.

Key points:

1. If you define only the getter method, it can be made read-only.

2. If you define only the setter method, it can be made write-only.

3. If you define both getter and setter methods, it can be made read-write. Let’s take another example program related to these key points.

In this example program, we will define only the getter method to read the value of the variables. We will declare a constructor to initialize the values of the variables. Look at the following source code step by step.

Program source code 2:

package gettersetterProgram;
 public class Employee 
  private String name; 
  private int id; 
  private String nCompany; 
 Employee(String name, int id, String nCompany)
 { = id; = name; 
   this.nCompany = nCompany; 
 public String getName() { 
   return name; 
public int getId() { 
   return id; 
public String getNCompany() { 
   return nCompany;
package gettersetterProgram;
public class  ReadOnlyTest { 
public static void main(String[] args)
 Employee em = new Employee("Deep", 23435, "TCS"); 
 String name = em.getName(); // This method will read the value of the variable. Since the return type of this method is String. That's why we will store the returning value using a variable 'name' with data type String. 

 int id = em.getId(); // The return type of this method is an integer. 
 String nCompany = em.getNCompany(); 
 System.out.println("Employee's name: " +name); 
 System.out.println("Employee's id: " +id);
 System.out.println("Company's name: " +nCompany);
        Employee's name: Deep 
        Employee's id: 23435 
        Company's name: TCS

Let’s take an example program where we will define only the setter method to set the values of the variable.

Program source code 3:

package gettersetterProgram; 
 public class Addition
   private int x, y, z; 
   public void setX(int a){ 
      x = a; 
 public void setY(int b) { 
   y = b; 
 public void setZ(int c) { 
    z = c; 
 void sum() { 
   int s = x + y + z; 
   System.out.println("Sum: " +s); 
public class MySum { 
public static void main(String[] args) 
 Addition a = new Addition(); 
       Sum: 60

Thus, the setter method is used to update or set the value of a variable whereas the getter method is used to read the value of a variable.

Hope that this tutorial has covered all the important points related to getter and setter method in Java with example programs. I hope that you will have understood better and enjoyed this tutorial.
Thanks for reading!!!

Next ⇒ Encapsulation in Java⇐ PrevNext ⇒

Leave a Comment