Java Modifiers
Words like public, private, static and final in front of a class, field or method are called modifiers. They change what the item is or who may use it. You have already used several of them; this topic puts them together.
Access modifiers
Access modifiers decide who can use a class, field, method or constructor:
| Modifier | Usable from |
|---|---|
public | everywhere |
protected | the same package, and subclasses |
| (none) | only the same package ("package access") |
private | only inside the same class |
A package is a group of related classes; the Packages topic shows how to make them. Subclasses come in Inheritance. For now the most important pair is public and private.
private: keep it inside
A private field can only be used by code inside its own class. Other classes get a compile error:
class BankAccount {
private double balance = 1000;
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.balance = -500;
System.out.println(acc.balance);
}
}Main.java:8: error: balance has private access in BankAccount
The compiler stops the bad change before the program can even run. To let other classes work with a private field in a controlled way, the class offers public methods:
class BankAccount {
private double balance = 1000;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.deposit(250);
acc.deposit(-999);
System.out.println(acc.getBalance());
}
}1250.0
The negative deposit is ignored, because deposit checks its input. This idea is called Encapsulation, the subject of the next topic.
Non-access modifiers
| Modifier | On a field | On a method | On a class |
|---|---|---|---|
static | one copy for the class | called without an object | (for nested classes) |
final | value set only once | cannot be overridden | cannot be extended |
abstract | – | has no body; subclasses provide it | cannot be created with new |
You met static and final fields in Class Attributes. abstract is covered in Abstraction. Modifiers can be combined, for example public static final.
Why main is public static void
Now every word of the main method makes sense:
public class Main {
public static void main(String[] args) {
System.out.println("public: Java can call it from outside the class.");
System.out.println("static: it runs without creating a Main object.");
System.out.println("void: it returns nothing.");
System.out.println("args: it receives command-line words, here " + args.length + ".");
}
}public: Java can call it from outside the class. static: it runs without creating a Main object. void: it returns nothing. args: it receives command-line words, here 0.
Choosing modifiers
A good default: make fields private, make the methods other classes need public, and keep helper methods private. Give the smallest access that works; it is always easy to open something up later and hard to close it once other code depends on it.
Practice exercises
- Make the fields of a
Studentclass private and try to read one fromMain. Read the error. - Add a public method
getName()and use it instead. - Make a private helper method inside a class and call it from a public method of the same class.