Java Access Modifiers
In this page:
public dataType field; // everywhere
protected dataType field; // package + subclasses
dataType field; // package only
private dataType field; // same class only
What are Access Modifiers?
Access modifiers control which parts of your codebase are allowed to see and use a given class, field, or method, forming the basis of encapsulation — they're the mechanism that actually enforces the 'hide internal details' principle of OOP.
Example: What are Access Modifiers?
// Define the class Account
class Account {
private double balance = 100;
public double getBalance() {
// Send balance back to the caller
return balance;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Account object and store it in acc
Account acc = new Account();
// Print a line to the console
System.out.println(acc.getBalance());
}
}
Login to try C/C++/Java/PHP code in the editor
The Private Modifier
private is the most restrictive level: a private member is visible only inside the class where it's declared, not even to subclasses. This is the default choice for internal implementation details you never want other code touching directly.
Example: The Private Modifier
class Account {
private double balance = 100;
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
// acc.balance would not compile: private to Account
System.out.println("Balance is private");
}
}
Login to try C/C++/Java/PHP code in the editor
Default Access
Leaving off a modifier entirely gives a member default (package-private) access, meaning any class in the same package can use it, but classes in other packages cannot — even subclasses outside the package are locked out unless they also use protected or public.
Example: Default Access
class Account {
double balance = 100; // no modifier: package-private
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
System.out.println(acc.balance); // visible: same package
}
}
Login to try C/C++/Java/PHP code in the editor
The Protected Modifier
protected opens visibility to the declaring class's own package plus any subclass anywhere, even in a different package — it's the modifier of choice for members a subclass legitimately needs to inherit and use, without exposing them to unrelated code.
Example: The Protected Modifier
class Vehicle {
protected String type = "Generic";
}
class Car extends Vehicle {
void printType() {
System.out.println(type); // subclass can access protected member
}
}
public class Main {
public static void main(String[] args) {
new Car().printType();
}
}
Login to try C/C++/Java/PHP code in the editor
Visibility Control
Choosing the tightest access level that still lets your code work is a core defensive-programming habit: it keeps your class's internal state safe from accidental external modification while still exposing exactly the operations callers actually need.
Example: Visibility Control
class Account {
private double balance = 100;
public double getBalance() {
return balance; // tightest access + controlled exposure
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new Account().getBalance());
}
}
Login to try C/C++/Java/PHP code in the editor
- Making a field
privateand accessing it from another class, which is a compile error. - Forgetting that no modifier means package-private access.
- Making everything
public, which breaks encapsulation.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: