Back to blogUse direct field access when the value is simple and unlikely to change. Use auto-encapsulation when the field may evolve into a calculation, validation, or rule.
December 10, 2025•5 min read•Marina
Auto-Encapsulation in Java: Useful or Exaggerated?
JavaOOPRefactoring
Auto-Encapsulation in Java: Useful or Exaggerated?
One common habit in Java is to always use getters and setters, even inside the class itself. But as Martin Fowler highlights in Refactoring, auto-encapsulating fields only makes sense when it actually adds value.
Sometimes it does. Sometimes it's just ceremony.
When Auto-Encapsulation Is Overkill
class Stock {
private double lastPrice;
public Stock(double lastPrice) {
this.lastPrice = lastPrice;
}
// Using getLastPrice() here adds nothing
public double applyDiscount(double pct) {
return lastPrice - (lastPrice * pct);
}
}`lastPrice` is a plain value. Calling `getLastPrice()` internally wouldn't improve clarity or flexibility, it would only add noise.
When Auto-Encapsulation Makes Sense
class StockHolding {
private int quantity;
private double pricePerShare;
public double getTotalValue() {
// This may evolve into a more complex rule
return quantity * pricePerShare;
}
public void printValue() {
System.out.println("Holding value: " + getTotalValue());
}
}Here, accessing the field through a getter is useful because the logic may grow: fees, adjustments, rounding rules, etc.
Centralizing the calculation in one method makes the class easier to maintain, exactly the kind of future-proofing Fowler describes.
To sum up
Encapsulation isn't about following a rule, it's about writing code that stays clean as it grows.