Java keyword this (power node)

We usually know that this represents the current instance of the method calling this class when using the this keyword in Java. Usually, it's easy to understand this keyword, but when I first learned, I couldn't understand a question clearly. Now I understand it slowly and want to write it down. Maybe someone has the same question as me, which may help others. Let's take a simple look at the role of this under normal circumstances. For example, the following code:

In the main method of the leaf class, we create a new leaf instance x, and then the X instance calls the increment () method. If increment () is an ordinary method or void method, there is nothing worth studying here. In particular, in the increment () method, we return this, which represents the X we just created. Because x is calling the increment () method, the increment () method this obviously represents the X instance of leaf.

There seems to be nothing to discuss. This is the instance X that calls the method. However, if we change the main () function to look like this

In the above modified code, we added and created a leaf instance y, and then y also called increment () twice in a row. Now the question is, if x and Y call the increment () method of at the same time, who can this represent? You may think there is something wrong with this. X calls the increment () method, this represents x, y calls the increment () method, and this represents y. The problem is that when we talk about calling methods, at the JVM level, we find the memory address of the increment () method in the leaf class, and then create the stack frame in the Java virtual machine stack

Then execute the code in the method in the stack frame. Now you see, that is, at the JVM execution method level, there are no so-called x calls and Y calls. Then, how exactly does this in the method determine which instance it points to?

Let's take a look at how the leaf class bytecode is displayed and whether we have missed anything. If we do not pass the X instance or Y instance into the method, it is impossible to know which instance this refers to when the JVM executes the method.

Here we see that there is no parameter in the encoding method in increment () method, but the number of parameters in the bytecode is 1. After careful consideration, the result is obvious: JVM will carry out a parameter in the instance method when executing the compilation, which is the result of the current call instance itself. For example, when x calls, X is passed to hide, and when y calls, y is passed to hide. Therefore, our this can determine who we are pointing to at the JVM execution method level.

The above conclusion is inferred by ourselves. Is there a detailed description of this in that book? In Java programming ideas, this is described as follows:

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