您可以在静态“内部”类中使用静态方法。
public class Outer { static String world() { return "world!"; } static class Inner { static String helloWorld() { return "Hello " + Outer.world(); } } public static void main(String args[]) { System.out.println(Outer.Inner.helloWorld()); // prints "Hello world!" }}但是,确切地说,
Inner根据JLS术语(8.1.3 )被称为嵌套类:
内部类可以继承不是编译时常量的静态成员,即使它们未声明它们也是如此。根据Java编程语言的通常规则,不是内部类的嵌套类可以自由声明静态成员。
同样,内部类不能具有成员也不是完全正确
static final。更精确地说,它们还必须是编译时常量。以下示例说明了区别:
public class InnerStaticFinal { class InnerWithConstant { static final int n = 0; // OKAY! Compile-time constant! } class InnerWithNotConstant { static final Integer n = 0; // DOESN'T COMPILE! Not a constant! }}在这种情况下允许使用编译时常量的原因很明显:它们在编译时内联。



