Encapsulation in Java: It's Not Just Making Fields Private

Ask most beginners what encapsulation is and you'll hear "making variables private and adding getters and setters." That answer isn't wrong, but it misses the point. Encapsulation is about making sure an object can never be put into an invalid state by outside code.
The problem: public fields
class Product {
public int stock;
}
Product p = new Product();
p.stock = -20;
This compiles and runs. Now your system has a product with negative stock, and nothing told you how it happened. Any code, anywhere, can change stock to anything, so every piece of code has to remember the rules. Someone will forget.
The fix: private state, controlled access
class Product {
private int stock;
public Product(int initialStock) {
if (initialStock < 0) {
throw new IllegalArgumentException("Stock cannot be negative");
}
this.stock = initialStock;
}
public void reduceStock(int qty) {
if (qty <= 0) {
throw new IllegalArgumentException("Quantity must be positive");
}
if (qty > stock) {
throw new IllegalStateException("Not enough stock");
}
stock -= qty;
}
public int getStock() {
return stock;
}
}
Three things changed:
stockis private, so outside code can't touch it directly- The constructor rejects invalid starting values
- Changes go through
reduceStock(), which enforces the rules in one place
Why this matters in real systems
Say the business adds a rule: reserved stock can't be sold. With a public field, you'd hunt through the whole codebase for every place that changes stock. With encapsulation, you update reduceStock() and every caller gets the new rule automatically.
It also makes bugs easier to find. If stock ever goes wrong, only the methods inside Product can be responsible, so you know where to look.
Common mistakes
- Getter and setter for everything. A class with a private field and a public
setStock(int)that does no validation is the same as a public field. Only add a setter if outside code genuinely needs to set the value, and validate inside it. - Returning mutable objects directly. If a class holds a
Listand its getter returns that list, callers can modify your internal state. Return an unmodifiable view instead:
public List<String> getTags() {
return Collections.unmodifiableList(tags);
}
- Exposing implementation details. Callers shouldn't need to know how you store data. If you change an
intto alonginternally, the public methods should keep working.
Interview question
"What's the difference between encapsulation and abstraction?"
A good short answer: encapsulation protects an object's internal state by controlling how it's accessed. Abstraction hides unnecessary detail and exposes only what the caller needs. Encapsulation is about safety, abstraction is about simplicity.
Takeaway
Don't ask "is this field private?" Ask "can any code put this object into an invalid state?" If the answer is yes, the class isn't encapsulated, however many getters it has.
About the author
- I break down Java and backend concepts into short, practical explanations, with the common mistakes and interview questions that come with each one. Writing as I learn, one concept at a time.
Comments
Join the discussionNo comments yet. Be the first to comment.

