Second, Java object-oriented (17) _ Inner class

Source: Internet
Author: User

2018-05-07

Inner class

You can define another class within one class.

Why use internal classes?

1) Enhanced encapsulation, hides the inner class inside the outer class, and does not allow other classes to access the inner class.

2) The inner class can improve the readability and maintainability of the code, embed the small class into the inner class, and the structure can be closer.

3) Internal classes can access external class members directly

The inner class is divided into four types according to the different modifiers or location of the position:

1) Instance inner class: the inner class does not use static adornments.

2) static inner class: The inner class uses the static modifier.

3) local inner class: The inner class defined in the method.

4) anonymous inner class: Suitable for use only once inner class, belongs to the special case of local inner class.

An inner class can be thought of as a member of an external class, like a field. The inner class can use public, default, protected, private adornments, and final adornments.

First, the instance inner class

An inner class that does not use the static modifier, which describes an inner class that belongs to an object of an external class, does not belong to the external class itself.

Characteristics:

1. Before creating an instance inner class, an external class object must exist to create an inner class object. (There must be an external class object when there is an inner class object)

Second, static internal class

Third, local inner class

Iv. Anonymous Internal classes

Second, Java object-oriented (17) _ Inner class

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.