JavaSE getting started 14: Java object-oriented internal class

Source: Internet
Author: User

JavaSE getting started 14: Java object-oriented internal class
1. Internal class overview

An Inner Class is a Class defined in another Class. Classes that contain internal classes are called external classes.

The main functions of internal classes are as follows:

A internal class provides better encapsulation. It can hide the internal class within the external class and does not allow other classes in the same package to access the class.

The internal class B method can directly access all data of the external class, including private data.

The functions implemented by the C internal class can also be implemented using the external class, but sometimes it is more convenient to use the internal class.

Internal classes can be divided into the following types:

A member internal class

B static internal class

Internal class of method C

D. Anonymous internal class

Member internal class

The most common internal class is the member internal class, also known as the common internal class. Let's look at the following code:

The running result is:

From the code above, we can see how to use the internal class of the member:

1) The Inner class is defined inside the Outer class, which is equivalent to the position of a Member variable of the Outer class. The Inner class can use any access controller, such

Public, protected, and private.

2) The test () method defined in the Inner class can directly access data in the Outer class without being affected by the access control operator, for example, directly accessing the data in the Outer class

Private property.

3) After defining a member's internal class, you must use an external class object to create an internal class object. Instead, you cannot directly create an internal class object, that is, an internal class object.

Class Object Name = External Class Object. new internal class ().

4) after the above program is compiled, two. class files are generated:

The second is the. class file of the external class, and the first is the. class file of the internal class, that is, the. class file of the internal class of the member is always like this:

Class Name $ internal class name. class.

Precautions for using external classes include:

A external class cannot directly use internal class members and methods. You can create an internal class object and then access its members through the internal class object.

Variables and methods.

B. If the external class and internal class have the same member variables or methods, the internal class accesses its own member variables or methods by default. If you want to access the external class

Can use this keyword. For example:

Running result:

Three static internal classes

Static internal classes are internal classes modified by static modifiers. These internal classes are characterized:

A static internal class cannot directly access non-static members of the external class, but it can be accessed through the new external class (). Member method.

B. If the static member name of the external class is the same as that of the internal class, you can use "class name. static member" to access the static member of the external class.

If the static member name of the external class is different from that of the internal class, you can directly call the static member of the external class using the "member name.

C. When creating static internal class objects without external class objects, you can directly create an internal Class Object Name = new internal class ():

Running result:

Four-way internal class

The internal class of the method is the method in which the internal class is defined as the external class. The internal class of the method is only visible within the method, that is, it can only be used in the method.

Note the following:

Because the internal class of the method cannot be used outside the external class, the internal class of the method cannot use the access controller or static modifier static

Non-static access is not allowed. Non-static access can be either static or non-static access.

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.