A brief introduction to the inner class of Java (the wrong place also please forgive me!) )

Source: Internet
Author: User
Tags modifier

In Java, an inner class is a class that is redefined within an outer class (that is, a class in a class). The class name does not need to be the same as the folder.

Internal classes are divided into: A: member Inner class, B: Local inner class, C: Static inner class, D: Anonymous inner class.

A: member inner class: the member inner class is the most common inner class, and it is defined as being inside another class. Just as a member of an external class, you can directly use all the members and methods of an external class, even if it is private. Although members of the inner class can access the members of the outer class unconditionally, the external class would like to access members of the member's inner class but not so arbitrarily. In an external class, if you want to access members of a member's inner class, you must first create an object of the member's inner class, and then access it by referring to the object

classOutter {Private intAge = 20; classInner {Private intAge = 21;  Public voidprint () {intAge = 22; System.out.println ("Local variable:" +Age ); System.out.println ("Inner class variable:" + This. Age); System.out.println ("External class variable:" + outter. This. Age); }    }}   Public classTest1 { Public Static voidMain (string[] args) {outter out=NewOutter (); Outter.inner in= out.NewInner ();    In.print (); }}
Results of the output:
Local variables: 22
Internal class variable: 21
External class variables:

It can be seen that the member inner class, which is a member of the outer class, can use all members and methods of the outer class directly, even if it is private. As I understand it, because the member inner class looks like a member of an external class, you can have multiple permissions adornments like members of a class. It is important to note that the member inner class cannot contain static variables and methods. Because the member inner class needs to create an external class before it can create its own

B: Local inner class

A local inner class is a class that is defined within a method and differs from the inner class of a member in that the access to the local inner class is limited to the method.

classOutter {Private intAge = 12;  Public voidPrint (Final intx) {classInner { Public voidInprint () {System.out.println (x);            System.out.println (age); }        }        NewInner (). Inprint (); }   }   Public classTest1 { Public Static voidMain (string[] args) {outter out=NewOutter (); Out. Print (10); }}

Results of the output:
10
12

In the above code we move the inner class into the method of the outer class, and then regenerate the inner class object in the method of the outer class to invoke the inner class method. If we need to pass in a parameter to a method in the outer class at this point, then the method parameter of the outer classes must use the final definition. In other words, the inner class defined in the method can only access local variables of the final type in the method, because the local variable defined in the method is equivalent to a constant whose life cycle is beyond the life cycle of the method run, and because the local variable is set to final, the value of the local variable cannot be changed in the inner class.

C: Static inner class

A static inner class is an inner class that is decorated as static. An inner class declared as static does not require a connection between an inner class object and an external class object, that is, you do not need to create an external class, and you do not need to create an inner class.

D: Anonymous inner class.

Anonymous inner class is actually a kind of local inner class, which is the simplified method of inner class. The precondition is that there is a class or interface (an anonymous inner class must have a relationship with a class or interface), which is essentially an anonymous object that inherits the class or implements the interface's subclass.

An anonymous inner class cannot have an access modifier and a static modifier.

Anonymous inner classes are the only classes that do not have constructors. Because it does not have a constructor, the use of anonymous internal classes is very limited.

A brief introduction to the inner class of Java (the wrong place also please forgive me!) )

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.