---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 17 Edition"
java_version: 17
chapter: "Chapter 14. Blocks, Statements, and Patterns"
section: "14.3"
heading: "14.3. Local Class and Interface Declarations"
parent: "Chapter 14. Blocks, Statements, and Patterns"
source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-14.html#jls-14.3
id: "14.3"
url: https://jvm.obfuscated.software/jls/se17/14.3.html
raw: https://jvm.obfuscated.software/jls/se17/14.3.md
---

# 14.3. Local Class and Interface Declarations

A *local class* is a nested class ([§8 (*Classes*)](8.md)) whose declaration is immediately contained by a block ([§14.2](14.2.md)).

A *local interface* is a nested interface ([§9 (*Interfaces*)](9.md)) whose declaration is immediately contained by a block.

```
LocalClassOrInterfaceDeclaration:
  ClassDeclaration
  NormalInterfaceDeclaration
```

The following productions are shown here for convenience:

```
ClassDeclaration:
  NormalClassDeclaration
  EnumDeclaration
  RecordDeclaration

NormalClassDeclaration:
  {ClassModifier} class TypeIdentifier [TypeParameters] [ClassExtends] [ClassImplements] [ClassPermits] ClassBody

EnumDeclaration:
  {ClassModifier} enum TypeIdentifier [ClassImplements] EnumBody

NormalInterfaceDeclaration:
  {InterfaceModifier} interface TypeIdentifier [TypeParameters] [InterfaceExtends] [InterfacePermits] InterfaceBody
```

Local class and interface declarations may be intermixed freely with statements (including local variable declaration statements) in the containing block.

It is a compile-time error if a local class or interface declaration has any of the access modifiers `public`, `protected`, or `private` ([§6.6](6.6.md)).

It is a compile-time error if a local class or interface declaration has the modifier `static` ([§8.1.1.4](8.1.1.4.md)), `sealed`, or `non-sealed` ([§8.1.1.2](8.1.1.2.md), [§9.1.1.4](9.1.1.4.md)).

It is a compile-time error if the direct superclass or a direct superinterface of a local class is `sealed`.

It is a compile-time error if a direct superinterface of a local interface is `sealed`.

A local class may be a normal class ([§8.1](8.1.md)), an enum class ([§8.9](8.9.md)), or a record class ([§8.10](8.10.md)). Every local normal class is an inner class ([§8.1.3](8.1.3.md)). Every local enum class and local record class is implicitly `static` ([§8.1.1.4](8.1.1.4.md)), and therefore not an inner class.

A local interface may be a normal interface ([§9.1](9.1.md)), but not an annotation interface ([§9.6](9.6.md)). Every local interface is implicitly `static` ([§9.1.1.3](9.1.1.3.md)).

Like an anonymous class ([§15.9.5](15.9.5.md)), a local class or interface is not a member of any package, class, or interface ([§7.1](7.1.md), [§8.5](8.5.md)). Unlike an anonymous class, a local class or interface has a simple name ([§6.2](6.2.md), [§6.7](6.7.md)).

The scope and shadowing of a local class or interface declaration is specified in [§6.3](6.3.md) and [§6.4](6.4.md).

**Example 14.3-1. Local Class Declarations**

Here is an example that illustrates several aspects of the rules given above:

```
class Global {
    class Cyclic {}

    void foo() {
        new Cyclic(); // create a Global.Cyclic
        class Cyclic extends Cyclic {} // circular definition

        {
            class Local {}
            {
                class Local {} // compile-time error
            }
            class Local {} // compile-time error
            class AnotherLocal {
                void bar() {
                    class Local {} // ok
                }
            }
        }
        class Local {} // ok, not in scope of prior Local
    }
}
```

The first statement of method `foo` creates an instance of the member class `Global.Cyclic` rather than an instance of the local class `Cyclic`, because the statement appears prior to the scope of the local class declaration.

The fact that the scope of a local class declaration encompasses its whole declaration (not only its body) means that the definition of the local class `Cyclic` is indeed cyclic because it extends itself rather than `Global.Cyclic`. Consequently, the declaration of the local class `Cyclic` is rejected at compile time.

Since local class names cannot be redeclared within the same method (or constructor or initializer, as the case may be), the second and third declarations of `Local` result in compile-time errors. However, `Local` can be redeclared in the context of another, more deeply nested, class such as `AnotherLocal`.

The final declaration of `Local` is legal, since it occurs outside the scope of any prior declaration of `Local`.
