Sub topic 10
Private Access Between Nested and Enclosing Classes
Assume you can write and compile basic Java classes, understand access modifiers, and distinguish instance members from static members. This chapter focuses on one rule: nested classes and their enclosing classes may access each other’s private members.
What private access means here
A private member is normally accessible only inside the class that declares it. A nested class is declared inside another class, but it is still a separate class. Java gives these related classes special access to each other’s private members.
This access works in both directions:
| Accessing class | Member being accessed | Allowed? |
|---|---|---|
| Nested class | Private member of the enclosing class | Yes |
| Enclosing class | Private member of the nested class | Yes |
| Unrelated class | Private member of either class | No |
The access does not depend on whether the nested class is static. Both static nested classes and non-static inner classes can use this rule.
public class Account {
private final String owner;
private int balance;
public Account(String owner, int balance) {
this.owner = owner;
this.balance = balance;
}
private void deposit(int amount) {
balance += amount;
}
public static class Auditor {
public static void inspect(Account account) {
System.out.println(account.owner);
account.deposit(50);
System.out.println(account.balance);
}
}
public static void main(String[] args) {
Account account = new Account("Mina", 100);
Auditor.inspect(account); // Mina, then 150
}
}Auditor can read owner, call deposit, and read balance, even though all three members are private in Account. The static nested class needs an Account object because those members are instance members.
Access in the other direction
The enclosing class can also access private members declared by a nested class. The nested class's private member remains hidden from unrelated callers.
public class Report {
private class Entry {
private final String text = "ready";
}
public void printEntry() {
Entry entry = new Entry();
System.out.println(entry.text);
}
public static void main(String[] args) {
new Report().printEntry(); // ready
}
}The printEntry method is inside Report, so it can access Entry.text. Code outside Report cannot directly name Entry because Entry is private, and it cannot access text either.
A private nested class is useful when an implementation type should support the outer class without becoming part of the outer class's public API.
How the compiler represents this access
Nested classes usually compile into separate class files. For example, these declarations commonly produce:
Account.classAccount$Auditor.class
The dollar sign is part of the generated class-file name. It does not make Auditor an ordinary top-level class for Java source access.
Modern Java uses nest-based access controls in the JVM. In older Java versions, the compiler could generate synthetic bridge methods so that one nested class could reach a private member of another class. Either way, source code follows the same rule: related nested and enclosing classes may access private members.
Design guidance
Use this access to keep closely related implementation details together. Do not make fields package-private merely because a nested helper needs them. Keeping them private preserves encapsulation from unrelated classes.
Still, extensive two-way access can make classes tightly coupled. If a nested class becomes large, reusable, or independently testable, consider making it a separate top-level class and exposing a narrow method-based interface instead.
Exercise
Can a static nested class directly read a private instance field of its enclosing class without an object?
Show answer
No. It has private access, but it still needs an enclosing-class instance, such as account.owner.