A getter reads a private field. A setter writes to it. Together they let you control how outside code interacts with your data:
class BankAccount {
private double balance;
public double getBalance() {
return balance;
}
public void setBalance(double amount) {
if (amount >= 0) {
balance = amount;
}
}
}
The getter returns the value without exposing the field directly. The setter includes validation: it rejects negative amounts. Without the setter check, any code could corrupt the balance.
Java convention names getters as getFieldName() and setters as setFieldName(). For boolean fields, the getter starts with is instead: isActive().