Javase Introductory Learning 14:java Object-oriented class

Source: Internet
Author: User

An internal class overview

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:

A inner class provides a better encapsulation that hides inner classes within an external class and does not allow other classes in the same package to access the class.

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

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

the inner classes can be divided into the following types:

A member inner class

b Static Inner class

C Method Inner Class

D Anonymous Inner class

two member Inner class

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


The result of the operation is:


As we can see from the code above, the use of the members ' inner classes is as follows:

1) The inner class is defined inside the outer class, which is equivalent to the position of a member variable of the outer class, and the inner class can use any access control character . as

Public, protected, private and so on.

2) The test () method defined in the inner class can directly access data in the outer class without being affected by the access control, such as direct access to the outer class .

Private property A.

3) After defining the member inner class, you must use an external class object to create the inner class object, instead of going directly to the new inner class object, namely: internal

Class object name = External class object. New inner Class ().

4) After compiling the above program, you will find that two. class files are generated:


The second one is the. class file for the outer class, the first of which is the inner class. . class file, which is the. class file of the member's inner class always like this: outside

The class name is the $ internal classes name.

Considerations for using external classes are:

A outer class cannot directly use members and methods of an inner class, you can first create an object of an inner class and then access its members through an object of the inner class

Change quantities and methods.


b if the outer class and the inner class have the same member variable or method, the inner class accesses its own member variable or method by default, if to access an external class

Member variable, you can use the This keyword. Such as:


Operation Result:


three static inner classes

The static inner class is an inner class that is modified by the static modifier, which is characterized by:

A static inner class cannot directly access a non-static member of an external class, but can be accessed by means of the new external class () member.

b 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 external class

Static member is not the same as the member name of the inner class, the static member of the outer class can be called directly from the member name.

c 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 ():


Operation Result:


Four method inner class

The inner class of the method is the inner class defined in the method of the outer class, and the inner class of the method is visible only within the method, which is only available in the method.


Some things to keep in mind are:

Because the method inner class cannot be used outside the method of the outer class, the method inner class cannot use the access control and the static modifier

Cannot access non-static, non-static can access both static and non-static.


Javase Introductory Learning 14:java Object-oriented class

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.