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

# 14.3. Local Class Declarations

A *local class* is a nested class ([§8 (*Classes*)](8.md)) that is not a member of any class and that has a name ([§6.2](6.2.md), [§6.7](6.7.md)).

All local classes are inner classes ([§8.1.3](8.1.3.md)).

Every local class declaration statement is immediately contained by a block ([§14.2](14.2.md)). Local class declaration statements may be intermixed freely with other kinds of statements in the block.

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

The scope and shadowing of a local class 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`.
