"Java Foundation" inner class, local inner class, anonymous inner class, static inner class, inner class in interface

Source: Internet
Author: User

Inner class:

1, the inner class defined in a class, the instantiation of the inner class is constructed with the method defined by the perimeter class, the inner class object has an implicit reference to the perimeter class, so the inner class can directly access the domain of the perimeter class of the perimeter class, including the private, which is the access privilege of the inner class, which is more powerful than the regular class

2. Inner classes can be hidden from other classes in the same package, but you can refer to the internal class declared by Outerclass.innerclass.

Local inner class:

1. Inner classes move inside the method, and the local inner classes not only have access to the external classes that define them, but can also access only local variables declared as final, compared to the inner classes.

2. The class name cannot be modified by public or private, default

3. The scope acts in the method that declares it, so even if other methods of this class do not know his existence.

Anonymous inner class:

1, omit the class name of the inner class, create only the object of this class, and become the anonymous inner class.

2, you will want to define a callback function, but do not want to write a lot of code, you can consider anonymous inner class.

3. Syntax:

Class outerclass{    //Peripheral class fields and methods   ... Supertype Object name =  New Supertype (construction params) {//When supertype is an interface, the construction parameter must be empty     //inner class domain and method ...     }}

4. Methods for obtaining anonymous inner classes: New Object (). GetClass (). Getenclosingclass ()

Static Inner class:

1, the internal class is static modification, so that access privileges are not, when the inner class does not need to access the outer class of the object, it is defined as a static inner class.

Internal classes in the interface:

1, the domain in the interface by default is public static final, so must be initialized immediately, and can no longer be assigned, the internal class defined in the interface is also default public static (the method is default to public)

"Java Foundation" inner class, local inner class, anonymous inner class, static inner class, inner class in interface

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.