The inner class of Java learning

Source: Internet
Author: User

Access rules for internal classes:

1. Inner classes can access members in external classes directly, including private

Members in an external class can be accessed directly because the inner class holds a reference to an external class, in the format: external class name. This

2. External classes to access internal classes, you must create an inner class object

Access format:

1. When the inner class is defined on the member position of the outer class and is not private, you can create the inner class object directly in the outer other class.

Format: outer class name. Internal class Name Variable name = External Class object. Inner Class object

Outer.Inner in=New Outer (). New Inner ();

2. When the inner class is in a member position, it can be decorated by the member modifier.

For example, private: encapsulates an inner class in the outer category

Static: The inner class has a static property (when the inner class is static decorated, only static members in the outer class can be accessed directly, and access restrictions occur.)

 Packagecom.dreamy.day03;/** * @authorDreamy **/classouter{Private Static intX=3; Static classinner{//static inner class        Static voidfunction () {SYSTEM.OUT.PRINTLN ("Inner:" +x);        }    }    voidmethod () {Inner in=NewInner ();    In.function (); }} Public classInnerclassdemo { Public Static voidMain (string[] args) {//Outer.Inner in=new Outer (). New Inner ();//in.function ();Outer.Inner.function (); }}

In other external classes, how do you directly access the non-static members of the static inner class?

New Outer.Inner (). function ();

In other classes outside, how do you directly access static members of static internal classes?

Outer.Inner.function ();

Note: When a static member is defined in an inner class, the inner class must be static.

Internal classes must also be static when static methods in an external class access the inner class

 Packagecom.dreamy.day03;/** * @authorDreamy **/classouter{Private Static intX=3; Static classinner{//static inner class        Static voidfunction () {SYSTEM.OUT.PRINTLN ("Inner:" +x); }    }    Static classinner2{voidShow () {System.out.println ("Inner2.show"); }    }     Public Static voidmethod () {//inner.function ();        New Inner2 (). Show (); }} Public classInnerclassdemo { Public Static voidMain (string[] args) {}}

Note: Direct access to members in the inner class, such as:

 Packagecom.dreamy.day03;/** * @authorDreamy **/classouter{Private intX=3; classinner{//Inner class        voidfunction () {SYSTEM.OUT.PRINTLN ("Inner:" +x); }    }    voidmethod () {Inner in=NewInner ();    In.function (); }} Public classInnerclassdemo { Public Static voidMain (string[] args) {Outer.Inner in=NewOuter ().NewInner ();    In.function (); }}

Use of inner classes: when describing things, there is something inside the thing, which is described by an inner class. Because internal transactions are using the contents of external things. Generally used in programming, the internal class is privatized, providing a public method to access

class body{          class  xinzang{          } public     void Show () {
New Xinzang ().
}}

The inner class of Java learning

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.