Java programming ideology note (6)-internal class, java programming ideology

Source: Internet
Author: User

Java programming ideology note (6)-internal class, java programming ideology

1. Definition
A class is defined inside another class. This class is called an internal class.

2. Link to the peripheral class
When an internal class object is created, it has a certain relationship with the peripheral object that creates it, so it can access all the members of the peripheral class without any special conditions.

How is it implemented?
Yes. When an internal class object is created using a peripheral class, the internal Class Object secretly captures a reference pointing to the peripheral class. Therefore, you can use this reference to access the members of the peripheral class.
Generally, these are all handled by the compiler, and we don't need to care about them.
This is precisely because, when we create an internal class object, it must be associated with the peripheral class object.

3. Use keywords. this and. new

. This keyword
Internal class to get reference of the current peripheral class object, you can use the. this keyword, pay attention to the difference with new

After you use. this, you can obtain the reference of the peripheral Class Object used when you create the internal class. If new is used, a new reference is created.

. New Keyword
If you want to directly create an internal class object instead of using the method of the peripheral class object, you can use the. new keyword.
It must be a peripheral Class Object. new, not a peripheral class. new

It is also important to note that it is impossible to create internal class objects (except for nested classes) before creating peripheral class objects ).

4. Internal class and upward Transformation
Transform the internal class to the base type, especially when the interface is used.

5. classes in the Method
You can create a class in the method.

It is worth noting that the class created in the method cannot contain an access modifier.
In addition, classes inside the method are not created when the method is called, and they are also compiled.

6. Anonymous internal class

The semicolon following the anonymous internal class is indispensable!
An anonymous class, as its name implies, has no name.

Anonymous classes can be created, interfaces, abstract classes, and common class objects. When creating an interface, all methods in the interface must be implemented.

The parameters accepted by the anonymous internal class must be final modified.

If it is not final, the compiler will prompt an error.
You can also define attributes in an anonymous internal class.
Because the class is anonymous, there is no constructor. If you want to imitate the constructor, you can use instance initialization ({})

7. Nested classes
Static internal classes are called Nested classes.
Note the following when using nested classes:
A. When creating nested class objects, no peripheral class is required
B. In a nested class, non-static members of the peripheral class cannot be accessed like normal internal classes.

In addition, the nested class has a special feature, that is, the nested class can have static methods, static fields and Nested classes, but not common internal classes.

8. Internal Class Identifier
We know that each class will generate a. class file, and the file name is the class name.
Similarly, the internal class will generate such. class file, but its name is not the class Name of the internal class, but has strict restrictions: the name of the peripheral class, plus $, and the name of the internal class.
The internal class defined in the method is not generated when the method is called, but compiled together with the peripheral class, which can be proved by viewing the. class file.

9. Why Internal classes?
A. The internal class provides a window for entering the peripheral class.
B. It is also the most attractive reason. Each internal class can inherit an interface independently, regardless of whether the peripheral class has inherited an interface.
Therefore, internal classes make the multi-inheritance solution more complete.
In a project, multiple inheritance is required. If there are two interfaces, the interface can be easily inherited.
What if there are two classes? In this case, only internal classes are used.

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.