Java Reference ยท Object-Oriented Concepts
Local classes, records, enums and interfaces
Declare classes, records, enums and interfaces inside a method: scope, captured variables, static context rules (Java 16+) and typical uses.
A local class is declared inside a method, constructor or initializer block, just like a local variable. It exists only in that block, so no other code can name it. Since Java 16 you can also declare local records, local enums and local interfaces, which are implicitly static and therefore cannot capture anything from the surrounding method.
Local types are useful for small helper shapes needed by one method: a pair of values to sort, a tiny state holder for a recursive search, or an intermediate step in a stream pipeline.
void report() { record Pair(String name, int score) { } // local record, Java 16+ var best = new Pair("Ada", 98); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Rules
| Rule | Detail |
|---|---|
| Scope | From the declaration to the end of the enclosing block. A local class cannot be used before it is declared. |
| Modifiers | No access modifier and no static. Only abstract and final are allowed. |
| Captured variables | A local class may read local variables and parameters that are final or effectively final. |
| Enclosing instance | A local class in an instance method has access to this of the enclosing object. In a static method it does not. |
| Local records, enums, interfaces | Implicitly static: they cannot capture locals or the enclosing instance. |
| Members | A local class may declare static members since Java 16. |
Usage notes
- Why effectively final? The class body may run later, after the method returns, so Java copies the captured values. Allowing reassignment would make the copy and the original diverge. Use an array or an
AtomicIntegerif you really need mutable captured state. - Prefer a local record over a local class when the type is only data. It is shorter and cannot accidentally capture state.
- Prefer a lambda when a functional interface fits. A local class is for cases where you need a name, several methods, fields, or to instantiate the type more than once.
- Naming: the compiler names it
Outer$1Name;getSimpleName()returns the declared name. - Local classes cannot be
publicand cannot be referenced from outside, so return them as an interface type if the method has to hand instances out. - Type parameters: a local record, enum or interface cannot use the enclosing method's type variables, because it is static. Declare its own type parameters instead.
- Hiding: a local class name may shadow a member class with the same name in the surrounding scope.