class A { private void g() { System.out.println("g()"); }
class B { void h() { System.out.println("h()"); g(); f(); } } }
public static void main(String[] args) { AcessMethod am = new AcessMethod(); AcessMethod.A a = am.new A(); AcessMethod.A.B b = a.new B(); b.h(); } } 运行结果:h()
g()
f() 问题1:在什么情况下可以定义static 方法?
只有在顶层类中定义,或者在静态内部类中定义,看下面的例子
public class Test { static void t(){} class T2{ //!错误,The method a cannot be declared static; //static methods can only be declared in a static or top level type //static void a(){} }