class 的词法封闭类型

Lexically inclosing type of a class

这就是 JLS 8.1.3 定义词法封闭类型概念的方式:

A class or interface O is the zeroth lexically enclosing type declaration of itself.

A class O is the n'th lexically enclosing type declaration of a class C if it is the immediately enclosing type declaration of the n-1'th lexically enclosing type declaration of C.

所以从这个定义中不清楚正在考虑任何内部 class 或只是 class 或接口 O[=19= 的内部 class ].这些是不同的概念,因为最后一个定义如下:

An inner class C is a direct inner class of a class or interface O if O is the immediately enclosing type declaration of C and the declaration of C does not occur in a static context.

A class C is an inner class of class or interface O if it is either a direct inner class of O or an inner class of an inner class of O.

为了更清楚,让我举个例子:

class A{
    class Bar{ } //inner class of class A

    public static void main(String[] args){
        class Foo{ } //inner class, but not an inner class of class A
    }
}

DEMO

立即封闭类型的定义被认为是任何内部 class 或 inner classes of class or interface O?

BarFoo 都有 A 作为直接封闭的 type 声明。他们是平等的。 Foo 也包含在 方法 声明中这一事实并不影响这一点。