Industry Ready Java Spring Boot, React & Gen AI — Live Course
JavaEncapsulation

Getters and Setters

Why Do We Need Getters and Setters?

Once we declare variables as private, they cannot be accessed directly from outside the class.
To read or modify these private variables safely, we use public methods.

These methods are known as getters (to retrieve data) and setters (to modify data).

Encapsulation relies heavily on these methods to ensure controlled access to internal data.

What Are Getters and Setters?

Method TypePurpose
GetterProvides read-only access to private variables
SetterAllows controlled modification of private variables

Getters and setters maintain data security and allow validation before updating any field.

methods

Example

class Student {
    private int rollNo;
    private String name;

    // Setter for roll number
    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;   // 'this' refers to the current object
    }

    // Getter for roll number
    public int getRollNo() {
        return rollNo;
    }

    // Setter for name
    public void setName(String name) {
        this.name = name;
    }

    // Getter for name
    public String getName() {
        return name;
    }
}

public class Demo {
    public static void main(String[] args) {
        Student s1 = new Student();

        s1.setRollNo(101);
        s1.setName("Kiran");

        System.out.println("Student RollNo: " + s1.getRollNo());
        System.out.println("Student Name: " + s1.getName());
    }
}

Output

Student RollNo: 101
Student Name: Kiran

Understanding the this Keyword

When method parameters have the same name as instance variables, this is used to differentiate between them.

Example:

public void setAge(int age) {
    this.age = age;
}

Here:

  • this.age → instance variable
  • age → method parameter

This avoids naming conflicts and makes the code clearer.

Why Not Access Variables Directly?

Allowing direct access leads to several issues:

  • No validation (someone may set age = -5)
  • Breaks encapsulation
  • Reduces control over how data is modified
  • Makes debugging and maintenance harder

Using setters allows controlled updates:

obj.setAge(-5);   // You can validate inside the method

Naming Conventions

Java follows standard naming rules:

VariableGetterSetter
agegetAge()setAge(int age)
namegetName()setName(String name)
rollNogetRollNo()setRollNo(int rollNo)

These conventions help frameworks and tools automatically recognize getter and setter methods.

Summary

ConceptDescription
EncapsulationHiding internal data and controlling access
Private variablesCannot be accessed directly
GetterReturns the value of a private variable
SetterUpdates a private variable with validation
this keywordRefers to the current object
BenefitData hiding, validation, flexibility, maintainability

Real-World Analogy

A vending machine stores items inside, but you cannot access them directly. You use buttons to request an item. These buttons act like getters and setters — providing controlled access to what’s inside.

Written By: Shiva Srivastava

How is this guide?

Last updated on