Private Fields with Getters and Setters in Java
Fast recall
Quick revision
Core answer
Whether you understand encapsulation as a design principle, not just a syntax rule to memorize
Generating getters and setters for every field automatically without thinking about whether each field should be mutable
Mention validation in setters as the real benefit, and explain that immutable fields should have getters only — no setter
Interview-ready guide
Interview answer
The reason fields go private is that a public field is an open door — anyone can set age to minus a hundred and the object is instantly broken with nothing to stop it. private means the only way in is a method I control.
That's what a setter really is: not the assignment, but the validation gate in front of it, rejecting garbage before it reaches the field. The getter is the read side, letting callers see the value without handing them a reference they can mutate — which also lets me change how the field is stored later without breaking anyone.
The point I'd stress is that not every field needs a setter; if something shouldn't change after construction, like an ID, I give it a getter only. A setter that just assigns with no checks buys almost nothing over a public field.
And for simple immutable data, I'd reach for a record instead of hand-writing all of it.
Private Fields with Getters and Setters in Java
Direct Answer
Making fields private enforces encapsulation — it stops external code from modifying state in unexpected ways. Getters give controlled read access; setters validate data before accepting a change. This protects data integrity and lets the internal implementation change without breaking callers.
Code Example
A realistic accessor does two jobs at once — it guards the value and it cleans it up. This Account rejects blank usernames and malformed emails, and it also normalizes whatever it keeps.
public class Account {
private String username;
private String email;
public String getUsername() { return username; }
public void setUsername(String username) {
if (username == null || username.isBlank())
throw new IllegalArgumentException("Username required");
this.username = username.trim(); // normalize
}
public void setEmail(String email) {
if (!email.contains("@"))
throw new IllegalArgumentException("Invalid email");
this.email = email.toLowerCase(); // normalize
}
}
Because callers can never write the field directly, every value that lands in username or email is already trimmed and lower-cased. That guarantee is only possible when the field is private and the setter is the single doorway in.
Common Mistakes & Pitfalls
- Auto-generating a getter and setter for every field — fields meant to be set once at construction end up with a setter they never needed, throwing away immutability for no reason.
- Writing setters with no validation —
this.age = age;with no checks is barely better than a public field; the validation is the entire reason the setter exists. - Returning the live reference from a getter —
return this.items;hands callers your internalList, and they can mutate it behind your back; return a copy or an unmodifiable view instead. - Confusing
privatewith secure — private is a compile-time visibility rule, not security; reflection can still reach the field, so this is about clean design and data integrity, not hiding secrets.
Check your understanding
Answer these without looking back at the article.
- What is the JavaBeans convention for getters and setters?
- Should every private field have both a getter and a setter?
- What happens if you make a field public?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.