Java OOP · Module 4
Getters, setters and validation
The controlled doors in and out of an object
What you'll be able to do
- Write getters and setters with the conventional Java names
- Choose between read-only, write-only and read-write access
- Implement four different validation strategies and say when each fits
- Explain why a setter without validation is a design smell
Getters & Setters
Accessors: get, is, set
Java has no built-in properties, so the community agreed on three prefixes. Follow them and every tool and teammate understands your class.
The naming convention (memorise it — tools depend on it)
x. Same type as the field, no parameters.boolean field: isActive(), not getActive().x. Takes one parameter, usually returns void — and should validate.Not every field needs both doors
| Access you expose | What you write | Typical example |
|---|---|---|
| read-write | getter + validating setter | a student's age |
| read-only | getter only | an account balance (changed only by deposit/withdraw) |
| write-only | setter only | a password hash you never hand back |
| none | neither | an internal cache or counter nobody outside needs |
Start with 'none' and add a door only when a real caller needs it. Generating getters and setters for every field by reflex is how encapsulation quietly disappears.
Notice there is no `setName`: the name is read-only. Try adding a validating `setName` that refuses an empty string.
Prefer behaviour over setters
account.setBalance(newValue) tells the reader nothing. account.deposit(100) and account.withdraw(50) describe *intent*, keep the rules inside, and make the audit trail obvious. Setters are for plain data; behaviour is for anything with rules.
Prove it — coding challenges
Validation
Four ways to refuse bad data
'Reject it' is not one decision — it is four, and the right one depends on who is calling.
Validation strategies, weakest to strongest
- Ignore silently —
if (amount <= 0) return;. Simple, but the caller never learns anything went wrong. Fine for demos, risky in real systems. - Clamp to a legal range — a volume above 10 becomes 10. Great for settings, wrong for money.
- Return a boolean —
boolean withdraw(double amount)returns false on refusal, so the caller can react. This is the style used in this course's challenges. - Throw an exception —
throw new IllegalArgumentException("amount must be positive"). The real-world default for programming errors, because it cannot be ignored by accident.
Predict all four lines. Then make the constructor pass `-1` and check that the object still starts at a legal price.
A refused change must change nothing
Check first, assign second. If you write balance = balance - amount; and *then* test for a negative balance, the object spends a moment in an illegal state — and any exception in between leaves it corrupted. The hidden tests in the challenges check exactly this.
Prove it — coding challenges
Common mistakes
public boolean getActive() { return active; }public void setAge(int age) { age = age; }Generating a getter and setter for every single field automatically.Lesson playground
A thermostat with a clamped range. Add an `isSafe()` method that returns true when the temperature is between 16 and 26.
Check understanding
Quiz
Question 1 of 5
This setter clamps instead of rejecting. What is printed?
Summary
- `getX()`, `isX()` for booleans, `setX(v)` — the convention every Java tool assumes.
- Expose the minimum: read-only, write-only or nothing is often the right answer.
- Validate by ignoring, clamping, returning a boolean, or throwing — pick deliberately.
- Always check before you mutate, and reuse the setter's rule inside the constructor.