Java Review Road 5

Source: Internet
Author: User
Tags class definition

An inner class (Inner Class) is a class that is defined in another class. Corresponding to this, the class containing the inner class is called the outer class.

The main functions of the inner class are as follows:

1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class

2. The method of the inner class can access all the data of the external class directly, including the private data

3. Functions implemented by internal classes can also be implemented using external classes, but sometimes it is easier to use internal classes

member Inner class (normal inner Class):

If an external class and an inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variables of the external class, you can use the This keyword. Such as:

Static inner classes are internal classes of static adornments, which are characterized by:

1. Static inner classes cannot directly access non-static members of an external class, but can be accessed through the new external class (). Members

2. If the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. static member; If the static member of the outer class is not the same as the member name of the inner class, the static member of the outer class can be called directly through the member name

3. When you create an object of a static inner class, you do not need an object of an external class, you can directly create an inner class object name = New inner Class ().

Method Inner class: The inner class of the method is the inner class definition in the method of the outer class, the inner class of the method is visible only within the method, that is, it can only be used inside the method.

Java Review Road 5

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.