Java Inner Class (4): Static inner Class & interface inner class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.