Sub topic 3
Public, Protected, Package-Private, and Private
Java controls which code can use a class’s fields and methods. A class is a blueprint for creating objects. An object is a value created from a class. A field stores a value in an object, and a method is an operation that code can call. Fields and methods are called members.
An access modifier is a Java keyword that sets where code may use a member. Java has three access modifier keywords: public, protected, and private. When a member has no access modifier, it has package-private access.
These choices matter because they let you make some operations available to other code while keeping other details inside the class.
Where access happens
A package is a group of Java classes. Classes in the same package can access some members that code in other packages cannot. A package declaration, such as package demo;, appears near the top of a Java file.
A subclass is a class built from another class. A subclass can use some members of the class it is built from. The protected modifier gives subclasses some extra access.
The four access levels
| Access level | Code in the same class | Other code in the same package | Subclass in a different package | Other code in a different package |
|---|---|---|---|---|
| `public` | Yes | Yes | Yes | Yes |
| `protected` | Yes | Yes | Sometimes | No |
| Package-private | Yes | Yes | No | No |
| `private` | Yes | No | No | No |
The table assumes the member’s class is accessible. Java modules can also limit access from outside a module. A module is a larger group of packages.
publicmakes a member available wherever its class can be accessed.protectedmakes a member available in the same package. It also allows some access from subclasses in other packages, but extra rules apply there.- Package-private access makes a member available only within its package. Write no modifier for this level;
package-privateis not a Java keyword. privategenerally makes a member available only inside the class that declares it. Enclosing and nested classes have special access to one another’s private members.
Try the modifiers
This example puts two classes in the same package. Meter has a member at each access level. Main uses the members that are available to it.
Save the code as Main.java:
package demo;
class Meter {
private int value = 0;
public int read() {
return value;
}
protected void reset() {
value = 0;
}
void increase() {
value = value + 1;
}
}
public class Main {
public static void main(String[] args) {
Meter meter = new Meter();
meter.increase();
System.out.println(meter.read());
meter.reset();
System.out.println(meter.read());
// meter.value = 10; // Not allowed: value is private.
}
}You need a Java Development Kit installed to run this example. Open a terminal in the folder containing Main.java. Compiling means checking and translating the Java code into a form Java can run.
javac -d . Main.java
java demo.MainThe output is:
1
0Here is what happened:
Maincalledincrease(). The method has no access modifier, so it is package-private. Both classes are indemo, so the call is allowed.Maincalled the publicread()method. The method can readvaluebecause both belong toMeter.Maincalledreset(). It is protected, andMainis in the same package asMeter.- The last line is a comment, so Java does not run it. If you remove the two slashes before
meter.value, the code will not compile:Maincannot directly access the private field.
Choosing a starting point
Use private for a member that other classes do not need to use directly. Use public for an operation you want other code to call. If access should stay within a package, consider package-private access. Use protected when you also want to allow some access from subclasses outside the package.
Before deciding whether a line can use a member, check the member’s modifier, the package containing the line, and whether the code is in a subclass. Also check that the member’s class itself is accessible.
Exercise
A class in the same package as Meter is not a subclass of Meter. Can it call Meter's protected reset() method?
Show answer
Yes. Protected members are accessible to other classes in the same package.
Exercise
In the example, why can read() use value when Main cannot use meter.value directly?
Show answer
read() is inside Meter, the class that declares the private field. Main is a different class.