Learn Java
import com.learnjava.oopfundamentals.AccessModifiers;

Access Modifiers in Java

@Concept

Access modifiers control which other code is allowed to see or use a class, field, method, or constructor. Java has four levels: public (visible everywhere), protected (visible within the same package, plus subclasses in other packages), package-private — the default when no modifier is written at all (visible only within the same package) — and private (visible only within the declaring class itself).

The general convention is to keep fields private and expose controlled access through public methods, getters and setters, instead of letting outside code reach in and modify a field directly. That indirection is what makes it possible to validate a new value, or change the internal representation later, without breaking any code that already depends on the class.

@HowItWorks
1

public

Visible and usable from anywhere, any class, any package.

2

protected

Visible within the same package, and additionally to subclasses even in a different package.

3

package-private (the default)

Writing no modifier at all means visible only within the same package, nowhere else.

4

private

Visible only inside the declaring class itself, not even to subclasses.

5

The getter/setter convention

Keeping fields private and exposing controlled public methods to read or change them allows validation and internal changes without breaking outside code.

@Example
public class Account {
private double balance;
 
public Account(double balance) {
this.balance = balance;
}
 
public double getBalance() {
return balance;
}
 
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
 
public static void main(String[] args) {
Account acc = new Account(100);
acc.deposit(50);
System.out.println("Balance: " + acc.getBalance());
}
}
Output
Balance: 150.0
1balance is private, so no code outside Account itself can read or write it directly.
2deposit(50) is the only way to change it from outside, and it validates the amount before touching balance at all.
3getBalance() is the only way to read it from outside, returning the field's current value.
4The single println call reads the updated balance through that public method, showing 150.0.
@UseCases
Preventing outside code from setting an object's field to an invalid value directly
Exposing a controlled, read-only view of internal data through a getter with no matching setter
Sharing helper methods within the same package without making them part of the public API
Understanding why a private field can't be accessed directly from another class, even a subclass

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.