为什么外部类在java中不是静态的?

在java中,外部类可以是public,final,default或abstract。 为什么不像静态一样

public static class MyClass{}

外部类已经隐式静态。

非静态嵌套类(=内部类)意味着内部类隐式地具有对其父类的引用。

这就是为什么,对于嵌套类,您可以区分静态和非静态。 这对外层阶级没有意义。

下面是一个了解静态/非静态嵌套类之间差异的示例。 你应该理解为什么它在外层阶级没有意义。

 public class MyClass { private String anAttributeOfMyClass; private /*static*/ class MyInnerClass { public void foo() { /* * Here, I can access the attribute of the parent class * because I implicitly have a reference to it. * Try to make the nested class static an see the difference. */ anAttributeOfMyClass.trim(); } } }