Internal classes that use static adornments are called static inner classes, but we prefer to call them nested inner classes . There is one big difference between static inner and non-static inner classes, and we know that a non-static inner class implicitly holds a reference after compilation, which points to the perimeter in which it was created, but does not have a static inner class. Without this reference, it means:
1, its creation is not required to rely on the peripheral class.
2. It cannot use non-static member variables and methods of any perimeter class.
1 InterfaceContents {2 intvalue ();3 }4 5 InterfaceDestination {6 String Readlabel ();7 }8 9 classTest005sub {Ten One Private Static classParcelcontentsImplementsContents { A Private inti = 11; - - Public intvalue () { the returni; - } - } - + Static classParceldestinationImplementsDestination { - PrivateString S; + A Privateparceldestination (String s) { at This. S =s; - } - - PublicString Readlabel () { - returns; - } in - Public Static voidf () { to } + - Static intx = 10; the * Static classAnotherlevel { $ {Panax Notoginsengx = 111; - System.out.println (x); the } + } A } the + Public StaticContents Contents () { - return Newparcelcontents (); $ } $ - Public StaticDestination Destination () { - return NewParceldestination ("Hello world!"); the } - }Wuyi the Public classTest005 { - Public Static voidMain (string[] args) { WuContents C =test005sub.contents (); -Destination d =test005sub.destination (); About NewTest005Sub.ParcelDestination.AnotherLevel ();//111 $System.out.println (C.value ());// One -System.out.println (D.readlabel ());//Hello world! - } -}
There is also an interface inner class that is placed in the interface
Any classes placed in an interface are automatically public and static , so placing nested classes within the namespace of an interface does not violate the rules of the interface.
1 InterfaceTest006sub {2 3 voidhowdy ();4 5 classTestImplementsTest006sub {6 Public voidHowdy () {7System.out.println ("howdy!");8 }9 }Ten } One A Public InterfaceTest006 { - Public Static voidMain (string[] args) { - NewTest006sub.test (). Howdy ();//howdy! the } -}
Java Inner Class (4): Static inner Class & interface inner class