Java Internal classes

Source: Internet
Author: User

/* Internal class Access features: 1, the inner class can access members in the outer class directly. 2, an external class must establish an inner class object to access the inner class. A design for the class. When analyzing a thing, it is found that there is something in the description of the thing, and that the thing is still accessing the content of the described thing. Then there are things that are defined as inner classes to describe. */class outer{private static int num = 31;class inner//inner class. {void Show () {System.out.println ("show Run ..." +num);} /*static void function ()//If a static member is defined in an inner class, the inner class must also be static. {System.out.println ("function run ..." +num); */}public void Method () {Inner in = new Inner (); In.show ();}} Class Innerclassdemo{public static void Main (string[] args) {//outer out = new Outer ();//out.method ();//Direct access to members in an inner class in an external class. Outer.Inner in = new Outer (). New Inner ();//in.show ();//If the inner class is static. Equivalent to an external class//outer.inner in = new Outer.Inner ();//in.show ();//If the inner class is static, the member is static. Outer.Inner.function ();}}
/* Why do internal classes have direct access to members of external classes? That's because the inner class holds references to external classes.  external class name. this*/class outer{int num = 3;class inner{int num = 4;void Show () {int num = 5; System.out.println (Outer.this.num);}} void Method () {new Inner (). Show ();}} Class InnerClassDemo2 {public static void main (string[] args) {new Outer (). method ();}}

  

 

Java Internal classes

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.