Java Encapsulation with Getters and Setters – Complete Guide with Examples


Learn Java encapsulation, how to hide data using private fields, and control access with getters and setters for secure, maintainable, and modular object-oriented programming.

Encapsulation and Getters/Setters in Java – Complete Detailed Tutorial

Encapsulation is one of the core OOP concepts in Java.

It is the technique of wrapping data (variables) and code (methods) together in a single unit (class) and restricting direct access to data from outside the class.

1. Why Encapsulation is Important

  1. Data Hiding: Protects variables from unauthorized access
  2. Controlled Access: Access only through methods
  3. Improved Security: Sensitive data like passwords are safe
  4. Code Maintainability: Changes to variables can be managed easily
  5. Better Modularity: Methods act as the interface to data

2. How to Implement Encapsulation

  1. Declare variables as private
  2. Provide public getter and setter methods to access and modify private data

Syntax:


class ClassName {
private dataType variableName; // private variable

// getter method
public dataType getVariableName() {
return variableName;
}

// setter method
public void setVariableName(dataType variableName) {
this.variableName = variableName;
}
}

3. Example – Encapsulation


class Employee {
private String name; // private variable
private double salary; // private variable

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

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

// Getter for salary
public double getSalary() {
return salary;
}

// Setter for salary
public void setSalary(double salary) {
if (salary > 0) { // validate before setting
this.salary = salary;
} else {
System.out.println("Salary cannot be negative");
}
}
}

public class Main {
public static void main(String[] args) {
Employee emp = new Employee();
emp.setName("Chinmaya");
emp.setSalary(50000);

System.out.println("Employee Name: " + emp.getName());
System.out.println("Employee Salary: $" + emp.getSalary());
}
}

Output:


Employee Name: Chinmaya
Employee Salary: $50000.0

4. Advantages of Using Getters and Setters

  1. Control Access: Can add validation before setting values
  2. Read-Only or Write-Only: Provide only getter or only setter
  3. Encapsulated Data: Reduces risk of unintended modification
  4. Flexible Maintenance: Modify internal implementation without affecting users

5. Example – Read-Only and Write-Only Fields


class Account {
private double balance;

// Read-only field
public double getBalance() {
return balance;
}

// Write-only field
public void setBalance(double balance) {
if (balance >= 0) {
this.balance = balance;
} else {
System.out.println("Invalid balance");
}
}
}

public class Main {
public static void main(String[] args) {
Account acc = new Account();
acc.setBalance(1000);
System.out.println("Balance: $" + acc.getBalance());
}
}

Output:


Balance: $1000.0

6. Best Practices for Encapsulation

  1. Keep fields private
  2. Provide public getters and setters
  3. Add validation in setters
  4. Use read-only or write-only access as needed
  5. Follow consistent naming conventions (getVariable, setVariable)

7. Summary

  1. Encapsulation → wrapping variables and methods together, restricting direct access
  2. Getters and Setters → public methods to access private variables
  3. Advantages: data hiding, validation, maintainability, security, modularity