Language syntax

Java static nested class

A member class without an implicit enclosing instance; can access private members through an explicit enclosing-class object. Multi-file example: save each labeled section separately. Compile the listed files with javac --release 21 -d out and run java -cp out demo.app.Main.

Formal Syntax & Structure

java syntaxjava
public static class LabelPrinter { /* members */ }

Working Example

Example Snippetjava
// File: src/demo/catalog/Shelf.javapackage demo.catalog;public class Shelf {    private final String label = "A2";    public static class LabelPrinter {        public String print(Shelf shelf) { return "Shelf " + shelf.label; }    }}
// File: src/demo/app/Main.javapackage demo.app;import demo.catalog.Shelf;public class Main {    public static void main(String[] args) {        Shelf shelf = new Shelf();        Shelf.LabelPrinter printer = new Shelf.LabelPrinter();        System.out.println(printer.print(shelf));    }}
Java Reference