Java Modifiers
Java modifiers are keywords that change how classes, fields, methods, and constructors behave. They can control who can access something, whether it belongs to an object or a class, and whether it can be changed.
You have already seen modifiers such as public and static in public static void main. In object-oriented code, modifiers help make classes clearer and safer to use.
Access Modifiers
Access modifiers control where code can be used from. The most common ones for beginners are public and private.
| Modifier | Meaning |
|---|---|
public |
Can be accessed from other classes. |
private |
Can only be accessed inside the same class. |
| No modifier | Can be accessed by classes in the same package. |
protected |
Can be accessed in the same package and by subclasses. |
For now, a good rule is to keep fields private and provide public methods when other code needs controlled access.
public And private
The next example uses a private field so code outside the class cannot change the balance directly. Instead, it must call public methods that contain the class’s rules.
class BankAccount {
private int balance;
BankAccount(int startingBalance) {
balance = startingBalance;
}
public void deposit(int amount) {
if (amount > 0) {
balance += amount;
}
}
public int getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(100);
account.deposit(50);
System.out.println("Balance: " + account.getBalance());
}
}
Output:
Balance: 150
The field balance is private, so it can only be used inside BankAccount. The methods deposit and getBalance are public, so main can call them.
The static Modifier
The static modifier means a field or method belongs to the class itself, not to one specific object. A static field is shared by all objects of that class.
class Player {
static int playerCount = 0;
String name;
Player(String name) {
this.name = name;
playerCount++;
}
void printName() {
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
Player first = new Player("Ava");
Player second = new Player("Leo");
first.printName();
second.printName();
System.out.println("Players: " + Player.playerCount);
}
}
Output:
Ava
Leo
Players: 2
Each Player object has its own name, but all Player objects share one playerCount. The expression Player.playerCount accesses the static field through the class name.
The final Modifier
The final modifier means a value cannot be reassigned after it has been set. It is often used for constants or for fields that should not change after construction.
class Course {
final String code;
static final int PASSING_SCORE = 70;
Course(String code) {
this.code = code;
}
boolean hasPassed(int score) {
return score >= PASSING_SCORE;
}
}
public class Main {
public static void main(String[] args) {
Course course = new Course("JAVA101");
System.out.println(course.code);
System.out.println(course.hasPassed(85));
System.out.println(course.hasPassed(60));
}
}
Output:
JAVA101
true
false
The field code is assigned in the constructor and cannot be changed afterward. The constant PASSING_SCORE is both static and final, so it belongs to the class and keeps the same value.
Modifier Order
Java allows several modifiers together. For readability, programmers usually write access modifiers first, then modifiers such as static and final. For example, public static final is the common order for a public constant.
Common Mistakes
- Trying to access a
privatefield directly from another class. - Using
staticfor data that should be different for each object. - Forgetting that a
finalvariable must be assigned before it is used. - Thinking
privatemeans hidden from objects. It means accessible only inside the class code.
Takeaway: modifiers help you control access, shared class data, and values that should not change.
