🧭Access Modifier
Also known as Access Modifiers, Public and Private, Visibility Modifier
A word like public or private that says which code may use a part.
Think of it like
It is like a shop with a front room and a staff-only room. Shoppers can only go in the front.
Example
A BankAccount class keeps balance private. Other code must call deposit(), which checks the amount first.
Why it matters
Definition
Example code
java
public class BankAccount {
private int balance;
public void deposit(int amount) {
if (amount > 0) balance += amount;
}
}Common mistake
Key takeaways
- 1public. Any code may use it.
- 2private. Only the class itself may use it.
- 3Guards the rules. Data changes only through methods.