Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A local class is a nested class defined entirely within a block of Java code, typically within a method body, constructor, or initialization block. It is scoped exclusively to the lexical block in which it is declared and cannot be referenced, subclassed, or instantiated outside of that specific execution context.

Syntax

public class EnclosingClass {
    private String instanceField = "Instance Data";

    public void enclosingMethod() {
        int effectivelyFinalVar = 42; // Must not be modified after initialization

        // Local Class declaration
        class LocalClass {
            public void displayState() {
                // Accessing enclosing class member
                System.out.println(instanceField);
                // Accessing effectively final local variable
                System.out.println(effectivelyFinalVar);
            }
        }

        // Instantiation is only valid within this method block
        LocalClass localInstance = new LocalClass();
        localInstance.displayState();
    }
}

Technical Characteristics

Scope and Modifiers
  • No Access Modifiers: A local class cannot be declared with access modifiers (public, private, protected). Its visibility is inherently restricted to the block that contains it.
  • No Static Declaration: A local class itself cannot be declared as static.
Variable Capture (Lexical Scoping)
  • Effectively Final Requirement: A local class can access local variables and parameters of the enclosing block only if they are declared final or are effectively final (their values are never reassigned after initialization). The compiler generates synthetic fields in the local class to capture these values.
  • Shadowing: Declarations of types, variables, or methods within a local class shadow declarations with the exact same name in the enclosing scope.
Enclosing Class Access
  • Instance Context: If the local class is defined within an instance method, it holds an implicit reference to the enclosing class instance (EnclosingClass.this). It can access all members of the enclosing class, including private fields and methods.
  • Static Context: If defined within a static method or static initialization block, the local class can only access static members of the enclosing class. It does not possess an implicit reference to an enclosing instance.
Static Members within Local Classes
  • Pre-Java 16: Local classes could not declare static members, with the strict exception of constant variables (variables declared static final of primitive or String types initialized with compile-time constant expressions).
  • Java 16 and Later: Local classes are permitted to declare static members, including static fields, methods, and interfaces, as part of the enhancements introduced by JEP 395 (Records).
Master Java with Deep Grasping Methodology!Learn More