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
- Data Hiding: Protects variables from unauthorized access
- Controlled Access: Access only through methods
- Improved Security: Sensitive data like passwords are safe
- Code Maintainability: Changes to variables can be managed easily
- Better Modularity: Methods act as the interface to data
2. How to Implement Encapsulation
- Declare variables as private
- 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
- Control Access: Can add validation before setting values
- Read-Only or Write-Only: Provide only getter or only setter
- Encapsulated Data: Reduces risk of unintended modification
- 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
- Keep fields private
- Provide public getters and setters
- Add validation in setters
- Use read-only or write-only access as needed
- Follow consistent naming conventions (
getVariable,setVariable)
7. Summary
- Encapsulation → wrapping variables and methods together, restricting direct access
- Getters and Setters → public methods to access private variables
- Advantages: data hiding, validation, maintainability, security, modularity