The inner class of Java learning

Source: Internet
Author: User
Tags local time

1. 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.

2. Access rules for internal classes:
1) The inner class can access the members of the external class directly, including the private.
Members in an external class can be accessed directly because the inner class holds a reference to an external class, in the form of an external class name. This
2) to access the inner class, the outer class must establish an inner class object.

3. Access format:

1) When the inner class is defined on the member position of the outer class and is not private, it can be in an external other class. You can create an inner class object directly.

The outer class name of the format. 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 the member position, it can be modified by the member modifier.

For example, private: Encapsulates inner classes in the outer class.

Static: The inner class has static properties. When an inner class is modified by static, only static members in the outer class can be accessed directly. Access restrictions have occurred.

3) How do you directly access the non-static members of the static inner class in other classes outside of the class? New Outer.Inner (). function ();

In other external classes, 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.

When a static method in an external class accesses an inner class, the inner class must also be static.

The code is as follows:

1 classOuter2 {3     Private Static intx = 3;4 5     classInner//Inner class6     {7         voidfunction ()8         {9System.out.println ("Innner Show");Ten         } One     } A  -     Static classInner2//static inner class -     { the         Static voidShow () -         { -System.out.println ("Inner2 Show::" +x); -         } +     } - } +  A classTest at { -      Public Static voidMain (string[] args) -     { -         //accesses members in an inner class.  -Outer.Inner in =NewOuter ().NewInner (); - in.function (); in         //accessing members of a static inner class - Outer.Inner2.show (); to     } +}


4) The inner class is defined at local time:

A) can not be modified by member modifiers
b) Members in an external class can be accessed directly because they also hold references in external classes. However, you cannot access the variables in the local area where it resides. Only the final decorated local variables can be accessed.

The code is as follows:

1 classOuter2 {3     intx = 3;4 5     voidMethodFinal inta)6     {7         Final inty = 4;8         classInner//internal classes defined in a local9         {Ten             voidfunction () One             { ASYSTEM.OUT.PRINTLN (x);//can access members in an external class -System.out.println (y);//only the final decorated local variable y can be accessed -System.out.println (a);//output passed in parameters the             } -         } -         NewInner (). function (); -     } + } -  + classInnerclassdemo A { at      Public Static voidMain (string[] args) -     { -Outer out =NewOuter (); -Out.method (7); -Out.method (8); -     } in  -}


4. Anonymous inner class

1) The anonymous inner class is actually the shorthand format for the inner class.
2) The premise of defining an anonymous inner class: An inner class must inherit a class or implement an interface.
3) Anonymous inner class format: New parent class or interface () {Defines the contents of a subclass}
4) In fact, the anonymous inner class is an anonymous subclass object. And the object is a little fat. Can be understood as an object with content.
5) It is best not to have more than 3 methods defined in an anonymous inner class.

The code is as follows:

1 Abstract classAbsdemo2 {3     Abstract voidShow ();4 }5 6 classOuter7 {8     intx = 3;9 Ten      Public voidfunction () One     { A         NewAbsdemo ()//Anonymous Inner class -         { -             intnum = 9; the             voidShow () -             { -System.out.println ("num===" +num); -             } + }.show (); -     } + } A  at classTest - { -      Public Static voidMain (string[] args) -     { -         NewOuter (). function (); -     } in}

The inner class of Java learning

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.