/* * this is static nested class */ privatestaticclassStaticNestedClass{ privatevoidyell(){ System.out.println(this.toString()); // OutterClass.this.yell();//静态内部类实例没有外部类实例的引用 } }
/* * this is inner class */ privateclassInnerClass{ privatevoidyell(){ System.out.println(this.toString()); OutterClass.this.yell();//内部类实例显式使用外部类实例的方式 } }
privatevoidrun(){ /* * this is local class */ classLocalClass{ publicvoidyell(){ System.out.println(this.toString()); } } /* * this is anonymous class */ new Object() { publicvoidyell(){ System.out.println(this.toString()); } }.yell(); LocalClass lc=new LocalClass(); InnerClass ic = new InnerClass(); StaticNestedClass sc=new StaticNestedClass(); lc.yell(); ic.yell(); sc.yell(); }
publicstaticvoidmain(String[] args){ OutterClass oc = new OutterClass(); oc.run(); } }