inner class

inner class

Classes defined inside other classes. Since the superior of the inner class is the outer class, the inner class can use three more modifiers than the outer class: private, protected, static.
From the JVM's point of view, inner classes are also independent classes. When compiling, each inner class will be compiled into a file similar to OuterClass$InnerClass.class, with the same level as the outer class. However, in each inner class an object reference of the outer class is kept. In this way, the data of the outer class can be accessed in the inner class, even the private data.

Classification of inner classes

1. Non-static inner class
2. Static inner class
3. Local inner class
4. Anonymous inner class

1. Non-static inner class

The non-static inner class looks no different from the ordinary non-static member in the outer class, and how the outer class treats other non-static members will also treat the non-static inner class. Also, don't use non-static inner classes in static methods of outer classes. After all, the attribution of static and non-static is different, one belongs to the class and the other belongs to the object.

1.1 Non-static inner classes are used within outer classes

Using a non-static inner class is no different from using any other normal class, using new to create member variables. And then no and then...

1.2 Non-static inner class is used outside the outer class

First of all, the non-static inner class can only be used within the access rights corresponding to the access control character, that is to say, if the inner class is private, then the outer class cannot be used anyway.
Since non-static inner classes are attached to specific objects like non-static member variables, there must be an outer object before a non-static inner class object, like thisOut.In in = new Out().new In();

2. Static inner class

In the same way, static inner classes can also be regarded as ordinary static members, and how the outer classes treat other static members will also treat static inner classes. Also, you cannot use outer non-static members in static inner classes. In the same way, one belongs to a class and the other belongs to an object, and they are not destined to be used together.

2.1 Static inner classes are used within outer classes

Using a static inner class is no different from using any other normal class, using new to create member variables. And then not and then...

2.2 Static inner class is used outside the outer class

First of all, the static inner class can only be used within the access rights corresponding to the access controller, that is to say, if the inner class is private, it cannot be used outside the outer class anyway.
Because a static inner class belongs to a class, when creating a static inner class object, you don't need to create an object of the outer class first, you can create it directly like this Out.StaticIn in = new Out.StaticIn();.

3. Local inner class

A class defined inside a method is only valid inside the method. To tell the truth, I feel that there should be very few opportunities for this inner class to be used. …

4. Anonymous inner class

Anonymous inner classes are suitable for classes that only need to be used once.
The format for defining an anonymous inner class is like this new 接口() | 父类构造器([参数]) {}. From this definition, it can be seen that anonymous inner classes are generally used to inherit a class or implement an interface. Because when the system creates an anonymous inner class, it will immediately create an object of the anonymous inner class, so the anonymous inner class cannot have abstract methods (the abstract methods cannot be instantiated), and the constructor (because the anonymous class does not have a class name) ).
There are also external local variables used by anonymous inner classes that must be modified with final (final is automatically added in Java 8).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073700&siteId=291194637