-1

Since we can not use the static modifier with a local class defined inside a method, and since Nonstatic nested classes are Inner classes, we could probably say that a method local class is a type of an Inner class.

But on the other hand, we say that instances of Inner classes CAN NOT exist without a Live instance of the enclosing class. But an instance of a method-local class defined in a static method CAN exist without a live instance of the enclosing class, right?

So what do I conclude from this? The first logic tells me that local classes are a type of Inner class, and the second piece of reasoning tells me that Local class is not an Inner class.

4

1 回答 1

2

我们说如果没有封闭类的 Live 实例,内部类的实例就不能存在

不,来自JLS(重点是我的):

如果 O 是 C 的直接封闭类型声明并且 C 的声明不在静态上下文中,则内部类 C 是类或接口 O 的直接内部类。

[...]

类或接口 O 的直接内部类 C 的实例 i 与 O 的实例相关联,称为 i 的直接封闭实例。

这告诉我们可能有一个没有封闭实例的内部类。特别是,这发生在它在静态上下文中定义的情况下。

于 2016-06-11T16:02:48.663 回答