Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Local class declaration in Java

A local class is a named nested class declared, for example, in a method block. The scope of its name does not limit the lifetime of its instances.

Version: Java SE 21

Syntax

{
    class LocalName {
        // Fields, constructors, and methods
    }
    LocalName instance = new LocalName();
}

Local class declaration is the syntax for defining a named nested class directly in a block, such as a method, constructor, or initializer body. For example, class Counter { ... } defines a local class. It is not a member of the enclosing class. Unlike an anonymous class, it has a simple name and you can instantiate it separately with new Counter().

The name is in scope throughout the declaration itself and the rest of the immediately enclosing block. In a switch block statement group, the scope ends with that group. Before the declaration or outside that scope, the name does not denote this class. An instance can outlive the block: return it as an IntSupplier and use it after the method ends.

A local class declaration cannot use public, protected, private, static, sealed, or non-sealed; its direct superclass and direct superinterfaces cannot be sealed. A normal local class is an inner class even when declared in a static method, where it has no enclosing instance. In Java SE 21, it can still declare its own static members. Local enum and record classes are implicitly static, so they are not inner classes and do not capture enclosing local variables or parameters.

Instance code in a normal local class can capture in-scope local variables and parameters only if they are final or effectively final. The compiler must also be able to prove that a captured local variable has been assigned before the class body. The step parameter is effectively final because it is never reassigned. The reference total cannot be reassigned, but total[0] can change: final does not make the array immutable.

Examples

import java.util.function.IntSupplier;

public class LocalClassDemo {
    static IntSupplier makeCounter(int step) {
        final int[] total = {0};

        class Counter implements IntSupplier {
            @Override
            public int getAsInt() {
                total[0] += step;
                return total[0];
            }
        }

        return new Counter();
    }

    public static void main(String[] args) {
        IntSupplier counter = makeCounter(3);
        System.out.println(counter.getAsInt());
        System.out.println(counter.getAsInt());
    }
}

Result

3
6

`Counter` captures the `step` parameter and the `total` reference. After `makeCounter(3)` returns, successive calls to `getAsInt()` add `3` to the same array element, so the program prints `3` and then `6`.

Related lessons

  • Java · Lesson 14 · Advanced · Version: 2
  • Java · Lesson 26 · Advanced · Version: 2
  • Java · Lesson 36 · Advanced · Version: 2
  • Java · Lesson 7 · Intermediate · Version: 2

Sources