Detailed explanation of internal classes of Java

Preface

A little classmate asked me to write a summary of this. I said, well, let's write and summarize the knowledge of this internal class. I feel that this will often be encountered in the interview. It's easy for everyone to forget the important basic knowledge of internal class, reflection, collection, IO flow, exception, multithreading and generics. I usually use less, so I often forget it. Now I write it down in a blog. If I forget it in the future, I can look back.

                                            --WZY

1、 What is an inner class

As the name suggests, an internal class is to define a class within a class. For example, if a class a defines a class B, class B is called an internal class relative to class A, and class A is an external class relative to class B.

There are many attributes in a class, such as member variables, member methods, local variables, static methods, etc. (these differences should be known. If you don't know, you should clarify these first, and then continue to look down. Yes, it's you), Class B can be divided into different internal classes at different positions in class A. There are four in total,

1. Member internal class

2. Local internal class

3. Static internal class

4. Anonymous inner class

It doesn't matter if you look confused now. Next, I will explain the four internal classes, usage and differences one by one

2、 Member inner class

1. What is a member inner class?

For example, the outer class has a private member variable ID and an out method. There is only one in method in the inner class (Note: there is no specific meaning in the written class, which is written to illustrate the problem)

2. How to instantiate member inner classes?

Analysis: it's not difficult to understand that an internal class object is created by an external class. If you want to use a method or attribute in a class, you must first have an object of that class. Similarly, if a class is inside another class, you must first have an instance object of the external class if you want to use this internal class, Then use the inner class through the object.

3. What can a member's internal class do?

· access all the attributes of the external class (the attributes here include private member variables and methods). Most of the examples are still the above contents, with a little change

What happens if the variable name in the inner class is the same as the member variable name of the outer class?

Summarize the general usage of internal classes (including 4 internal classes) with the help of member internal classes:

1. To access the contents of the internal class, you must instantiate the internal class through the external class object.

2. It can access all properties and methods of the external class. The principle is that when the internal class object is instantiated through the external class object, the external class object passes its own reference into the internal class, so that the internal class can be used through outer This is usually called implicitly to call the properties and methods of the external class. However, when the property or method name in the internal class is the same as that in the external class, you need to explicitly call outer This.

4. Internal interview questions

Answer: num, this num、Outer. this. num

Analysis: how do you understand the second point in the above summary? It's very simple. It examines 1, local variables 2, this, and 3, outer This is the principle of internal class accessing external class attribute methods. These three things,

1. In a method, use the variable name directly. It must use local variables, because large member variables will be hidden. In this problem, that is, Num in the show method will hide the member variable num in the internal class, and num in the internal class will hide the member variable num in the external class, Therefore, by directly outputting num, it will be the value 30 of the local variable in the show method

2. Use this Num calls. This represents the object that calls the method. Oi calls the show method. OI is the internal class object, so OI Num identifies the value 20 of the member variable num of the inner class

       3、Outer. this. Num, the value of the member variable num in the called external class is 10. If this is not clear, see the second point in the above summary, which is the principle.

3、 Local inner class

1. What is a local internal class? Like local variables, it is defined in a method, so it is a local internal class.

For example

2. The general functions of local internal classes are similar to those summarized in member internal classes, but there are two things to pay attention to,

1. In a local internal class, if you want to access a local variable, the local variable should be decorated with final. Look at the example

Why do I need to use final?

Final modified variable: when it becomes a constant, it will be placed in the constant pool,

Think about this problem in reverse. If final modification is not applied, when the local internal class is instantiated, the method pops up the stack and the local variable disappears. At this time, the local internal class object will report an error when it wants to call the local variable, because the local variable has disappeared. When the local variable is modified with fanal, It will be added to the constant pool. Even if the method bounces the stack, the local variable remains in the constant pool, and the local internal class is enough to call. Therefore, when a local internal class wants to call a local variable, it needs to use the final modifier. If it is not used, the compilation degree cannot pass.

2. Local internal classes cannot be instantiated directly through external class objects, but instantiate themselves in methods, and then call methods in their own classes through internal class objects. See the following example to know how to use it

Summary: for local internal classes, just pay attention to the bright spot

1. In a local internal class, if you want to access a local variable, the local variable should be decorated with final

2. How to call local internal class methods.

4、 Static inner class

According to the name, internal classes decorated with static are called static internal classes.      

Review the usage of static: generally, only variables and methods can be modified. Normally, classes cannot be modified, but internal classes can be modified by static.

1. Static modifies member variables: instances of the whole class share static variables

2. Static modified methods: static methods can only access static modified properties or methods, while non static methods can access static modified methods or properties

3. Member variables and methods modified by static can be called directly by the class name.

4. Static cannot modify local variables. Remember not to be confused. Static is usually used to modify member variables and methods.

Note:

1. The internal class mentioned above can call the methods and properties of the external class, just in the static internal class, because the static internal class has no reference to the external class object. Unless the method or property in the external class is also static. This returns to the usage of the static keyword.

2. Static internal classes can be instantiated directly by external classes without using external class objects

              Outer. Inner inner = new Outer. Inner();

3. What static methods and static variables can be used in static inner classes, but static methods and static variables cannot be declared in non static inner classes

5、 Anonymous inner class

This is the most used of the four internal classes. It is the most common program encountered in the future.

What is an anonymous object? If an object is used only once, we need new object() method()。 You don't need to save this instance to this type variable. This is the anonymous object. For example

Anonymous inner classes are the same as anonymous objects,

Anonymous object: I only need to use it once, so I don't have to declare a variable of this type to save the object,

Anonymous inner class: I only need to use it once, so I don't need to define an inner class in the class first, but wait for it to be used. I'm temporarily implementing this inner class, because it's used less times, maybe this time, so it's more convenient to write the inner class like this. Otherwise, write out all the implementations of an internal class first, and then call it once. Don't you always put it there after you use it up? That's not necessary.

How is an anonymous inner class formatted?

Premise: there is a class or interface. Why? For example: a interface new a() {}; All abstract methods in interface a must be implemented. After implementation, the whole of new a() {} is an anonymous implementation class object of A. you can certainly call the methods in your own class. It is not necessarily an interface, but an abstract class or a complete class. If it is an abstract class, you can implement the abstract methods just like the interface, If it is a complete class, you can override the methods you need, and then call,

For example, look at the difference between not using anonymous inner classes and using anonymous inner classes

It's not practical to use anonymous inner classes. It's really troublesome.

Anonymous inner class used.

Parsing: in fact, just understand that new test1() {implements the code of the method in the interface}; Test1(){...} The function of this is to implement the interface, but the interface is implemented by an anonymous class, that is, the class has no name and can only be used this time. We know that this is a class. If we new it, we can obtain an instance object of a class that implements the test1 interface. Through this instance object, we can call the methods in this class, Because its anonymous class is implemented in a class, it is called an anonymous inner class. Don't worry about why test1() {...} It is equivalent to realizing the test1 interface. The principles are powerful enough. Don't drill the horn when learning. Here, you just need to know what its role is and what it has done.

Interview questions of anonymous internal class:

Answer:

Analysis: this topic is very interesting. I tested a lot of things. At the beginning, I can only say that I was confused and forced. It is a local anonymous internal class. By looking at the call in the main method, outer Method() can directly call the method with the class name, so it must be a static method, and then directly call the show() method, indicating that the method method has a return value, and its return value type is the type that implements the interface class, because only the show() method is available in the interface. So in method, it is an anonymous inner class.

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
分享
二维码
< <上一篇
下一篇>>