Java internal classes and their principles

Implementing internal classes in Java

I believe we have used the internal class many times, let alone how it is used.

Inner class

1. Member internal class

It should be noted that when the internal class of a member has a member variable or method with the same name as the external class, the members of the internal class are accessed by default. If you want to access the members of the external class with the same name, you need to use the following form:

外部类.this.成员变量外部类.this.成员方法

The internal class is dependent on the external class, that is, to create the object of the member's internal class, the premise is to create an external class object. The method of creating the member's internal class is as follows:

new Main().new Inner();

Member internal classes can have private access, protected access, public access and default access. If it is decorated with private, it can only be accessed inside the external class.

2. Local internal class

A local inner class is a class defined in a method or scope, and its access rights are limited to within the method or scope.

Local inner classes can also be returned, such as:

3. Anonymous inner class

Anonymous inner classes should be the most commonly used, such as the following thread creation:

The anonymous inner class is automatically named by the system when compiling: Main $1

Anonymous inner classes are classes without constructors. Most of them are used to inherit other classes or implement interfaces. There is no need to add additional methods, but only the implementation or rewriting of inherited methods

4. Static internal class

Static internal classes are also classes defined in another class, but static is added in front of the class. Static internal classes do not need to depend on external classes, which is similar to static member variables.

When the static class is created externally, it can be created as follows:

Main.Inner mi = new Main Inner();

Internal class implementation principle

Why can an inner class access members of an outer class?

The internal classes are defined as follows:

Decompile using the javap command.

Compile to get main class Main$Inner. Class two files, decompile main $inner The class file is as follows:

You can see that the inner class actually has a reference to the outer class, and the reference to the outer class is passed in the constructor.

Why can anonymous inner classes only access local final variables?

In fact, you can think like this. After the method is executed, the life cycle of the local variable ends, and the life cycle of the local internal class object may not end, so it is impossible to access the local variable in the local internal class. Therefore, change the local variable to final and change its life cycle.

The code is as follows:

This code is compiled as main class Main$1. Class two files, decompile main $1 The class file is as follows:

You can see that Java directly copies and replaces the values determined at compile time, puts the values that cannot be determined into the constant pool of the internal class, and takes them out of the constant pool into the field in the constructor.

It can be seen that Java directly copies the local variable m, so it is not the original value. If M is changed in the internal class, the m value of the local variable will not change, and there will be data inconsistency. Therefore, Java limits it to final so that it cannot be changed. In this way, the problem of data inconsistency is solved.

Why do anonymous inner classes access external class member fields without final?

As mentioned above, the final keyword is used to solve the problem of inconsistent data. Because there are references to external classes in the internal class, all modifications to fields in the external class will be truly reflected in the external class instance itself, so it does not need to be modified with final.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>