Sub topic 11
What Nested Classes May Declare
Assume you know basic Java classes, access modifiers, instance and static members, interfaces, and method overriding. This chapter focuses on the rules that limit declarations inside nested classes.
A nested class is a class declared inside another class or interface. Java supports four common forms:
- A static nested class belongs to the enclosing type, not to an enclosing object.
- An inner class is a non-static member class tied to one enclosing object.
- A local class is declared inside a method, constructor, or initializer.
- An anonymous class is an unnamed class created at the point where it is needed.
Members of a nested class
A nested class can usually declare the same members as a top-level class:
- Fields
- Constructors
- Methods
- Nested classes
- Interfaces
- Initializer blocks
- Type parameters
- An
extendsorimplementsrelationship
Its access depends on where it is declared. A member nested class may be public, protected, package-private, or private. A local or anonymous class cannot use these member access modifiers because it is scoped to a code block.
public class Library {
private static class BookRecord {
private final String title;
BookRecord(String title) {
this.title = title;
}
String title() {
return title;
}
}
public static void main(String[] args) {
BookRecord book = new BookRecord("Java Basics");
System.out.println(book.title());
}
}BookRecord is private, so code outside Library cannot name it. The enclosing class can still create and use it.
Static declarations and inner classes
A static nested class can declare static and instance members normally. It does not need an enclosing object.
An inner class has an implicit reference to an enclosing instance. Older Java versions restricted static members in inner classes. Modern Java allows many static declarations in an inner class, but a static initializer is still prohibited. Code that must run once for the inner class should usually be moved to a static nested class or a static method in the outer class.
public class Outer {
class Inner {
static final int VERSION = 1;
static String label() {
return "inner";
}
// static { } // Compile-time error: static initializer
}
static class Utility {
static {
System.out.println("Utility initialized");
}
}
public static void main(String[] args) {
System.out.println(Inner.label());
System.out.println(Utility.class.getName());
}
}The exact rules can matter when compiling with older source compatibility settings. Follow the Java version used by your project.
Special nested declarations
Some declarations are implicitly static when nested:
| Declaration | Nested behavior |
|---|---|
| Class declared with `static` | Static nested class |
| Enum declaration | Implicitly static |
| Record declaration | Implicitly static |
| Interface declaration | Implicitly static |
Because nested enums, records, and interfaces are static, they cannot directly use an enclosing object's instance fields or methods.
public class Order {
enum Status {
NEW, PAID
}
record Item(String name, int quantity) {
}
interface Validator {
boolean valid(String value);
}
public static void main(String[] args) {
Item item = new Item("Notebook", 2);
System.out.println(Status.NEW + " " + item);
}
}A nested record cannot be declared as an inner class. If a record needs data from an enclosing object, pass that data explicitly.
Local and anonymous restrictions
A local class can declare fields, methods, constructors, and nested declarations allowed by the Java version. Its name is visible only inside the enclosing block.
An anonymous class has no class name and cannot declare an explicit constructor. Instance initialization blocks and fields are possible, but a named class is clearer when the implementation needs substantial state or behavior.
Exercise
Why can a nested record not directly read an enclosing class's instance field?
Show answer
A nested record is implicitly static, so it has no enclosing object reference. Pass the required value through a record component or method argument.