Java Internal classes

Source: Internet
Author: User

 PackageInnerclassdemo;/*** Java member inner class * * The 1.Inner class is defined inside the outer class, equivalent to the position of a member variable of the outer class, and the inner class can use any access control such as public, private, protected * 2. The test () method defined in the inner class can directly access data in the outer class without being affected by the access modifier, such as direct access to the private property in the outer class, A * 3. After you define a member inner class, you must use an external class object to create an inner class object. Instead of just going to new an inner class object, that is, the inner class object name = Outer class object. New inner class (); * 4. After compiling the above program, it is found that two. class files are generated *@authorGX * NOTE: * External classes cannot directly use the member methods in an inner class, you can create an inner class object, and then access the member variables in the inner class through the object of the internal classes * External classes and inner classes have the same member variables or methods, and internal classes access their member variables or methods by default, if you want to access the members of the outer Can use the This keyword **///External class outer Public classOuter {Private inta=99;  Public classinner{intb =2;  Public voidTest () {System.out.println ("Access a: In an external class" +outer. This. a); System.out.println ("Access B in inner class:" +b); }    }    //Test member Inner class     Public Static voidMain (String[]args) {Outer o=NewOuter (); Inner I=o.NewInner ();    I.test (); }}

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.