Java Encapsulation
In this page:
What is Encapsulation?
Encapsulation bundles an object's data (fields) and the code that operates on it (methods) into one unit, then restricts direct outside access to that data — acting like a protective shell around the object's internal state.
Example: What is Encapsulation?
class Account {
private double balance; // data and methods bundled together
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
acc.deposit(50);
System.out.println(acc.getBalance());
}
}
Login to try C/C++/Java/PHP code in the editor
Private Data Fields
Marking fields private hides them completely from code outside the class, which is the mechanism encapsulation actually relies on — without private fields, there's nothing stopping external code from reaching in and corrupting an object's state directly.
Example: Private Data Fields
class Account {
private double balance = 100;
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
// acc.balance would not compile: private field
System.out.println("Field is hidden from outside code");
}
}
Login to try C/C++/Java/PHP code in the editor
Getter and Setter Methods
Getters and setters give controlled, indirect access to those hidden private fields, and because a setter is real code (not a bare assignment), it can validate incoming values — rejecting bad data — before it's ever allowed to update the field.
Example: Getter and Setter Methods
class Account {
private double balance;
public void setBalance(double balance) {
if (balance >= 0) { // validation
this.balance = balance;
}
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
acc.setBalance(-10);
acc.setBalance(200);
System.out.println(acc.getBalance());
}
}
Login to try C/C++/Java/PHP code in the editor
Read-Only and Write-Only Classes
Skipping setters produces an effectively read-only class (state can be inspected but never changed after construction), while skipping getters produces a write-only class — both are useful patterns depending on whether callers need to see or only set a value.
Example: Read-Only and Write-Only Classes
class ReadOnly {
private final int id = 7;
public int getId() { return id; } // no setter
}
public class Main {
public static void main(String[] args) {
System.out.println(new ReadOnly().getId());
}
}
Login to try C/C++/Java/PHP code in the editor
Benefits of Encapsulation
Well-encapsulated classes protect against invalid state, are much easier to unit test in isolation, and let you freely refactor internal implementation details later without breaking any external code, since callers only ever depend on the stable public getter/setter contract.
Example: Benefits of Encapsulation
class Account {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount; // protects against invalid state
}
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
acc.deposit(-50); // rejected
acc.deposit(100);
System.out.println(acc.getBalance());
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: