Course contentsShow
← Java
Lesson 617 of 2,87015. Nested and Inner ClassesFree lesson

Access Modifiers for Static Nested Classes

Understand how private, protected, and public modifiers control visibility of static nested classes.

Access Modifiers for Static Nested Classes

What you'll learn: How private, protected, and public modifiers control who can see and use your static nested classes.

The Core Idea

Just like regular classes, fields, and methods, static nested classes can have access modifiers that determine their visibility. The modifier you choose controls which parts of your program can create instances of the nested class or even know it exists.

Think of it like rooms in a building: a public room is open to everyone, a private room is only for people inside the main building (the outer class), and protected extends access to related buildings (subclasses and the package).

The Four Visibility Levels

public static nested class: Accessible everywhere. Anyone can instantiate it using OuterClass.NestedClass.

protected static nested class: Accessible within the same package and by subclasses of the outer class (even in other packages).

Default (no modifier) static nested class: Accessible only within the same package. Classes outside the package cannot see it.

private static nested class: Only the outer class itself can access it. This is common for implementation details—helper classes that organize code internally but aren't part of the public API.

Why This Matters

Access modifiers let you hide implementation details. A private static nested class might organize complex logic internally without exposing that structure to the rest of your codebase. A public one becomes part of your class's contract with the outside world.

Key Takeaway: Access modifiers on static nested classes work just like on any other class member—use private to hide internal details, public to expose them, and protected or default for intermediate visibility.