This in java can be seen everywhere and has many uses. Under normal circumstances, it is easy to understand this keyword. However, when I first started learning, I had a question that I could not clearly understand. Now I slowly understand it. I will record it for everyone through this article for reference by friends in need. Next
When we usually use the this keyword in Java, we all know that this represents the current instance of the method that is calling this class. Under normal circumstances, it is easy to understand this keyword, but when I first started learning, I had a question that I couldn't clearly understand. Now that I understand it slowly, I want to write it down. Maybe someone has the same problem as me. Question, maybe it can help others. Let’s first briefly look at the role of this under normal circumstances. For example, the following code:
public class Leaf { private int i = 0; Leaf increment() { i++; return this; } void print() { System.out.println("i = " + i); } public static void main(String[] args) { Leaf x = new Leaf(); x.increment().increment().print(); } }
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 a normal method or void method, there is nothing worth studying in this place. What's special is that in the increment()
method, what we return is this, and this this represents the x we just created. Because x is calling the increment() method, increment()
method this obviously represents the x instance of Leaf.
There seems to be nothing to discuss. This represents the instance x that calls the method. However, if we modify the main()function to look like the following
public static void main(String[] args) { Leaf x = new Leaf(); x.increment().increment().print(); Leaf y = new Leaf(); y.increment().increment().print(); }
In the above modified code, we create a new Leaf instance y, and then y also calls two consecutive calls. timesincrement(). Now the question is, if x and y call the increment() method at the same time, then who does this represent? You may think there is something wrong with this. When x calls the increment() method, this represents x. When y calls the increment() method, this represents y. But the problem is that when we talk about calling methods, at the jvm level we find the memory address where the increment()
method in the Leaf class is located, and then create a stack frame in the java virtual machine stack.
Then execute the code in the method in the stack frame. Now you see, that is to say, at the jvm execution method level, there is no so-called x call, y is called. So, how does this in the method determine which instance it points to?
Let’s take a look at how it is displayed in the Leaf class bytecode to see if we have missed anything. If we do not pass the x instance or y instance into the method, then, execute the method in the jvm , it is impossible to know which instance this specifically points to.
Here, we see that in the increment()
method, there are no parameters in the encoding, but the number of parameters is shown as 1 in the bytecode. Think about it carefully , the result is already obvious: when JVM performs compilation, in the instance method, a parameter will be passed hidden by default, and this parameter is the currently called instance itself. For example, if x is called, x will be passed when hidden, and if y is called, y will be passed. Therefore, our this can determine who it points to at the jvm execution method level.
The above conclusion is our own inference. Is there any book that describes this in detail? In "java Programming Thoughts", this section is described as follows:
Suppose we are inside a method and want to get the handle of the current object . Since that handle is passed "secretly" by the compiler, no identifier is available. However, there is a dedicated keyword for this purpose: this.
The handle secretly passed by the compiler mentioned in it is the hidden parameter we have here.
So far, the description of this must be very clear, and we understand it at the jvm level. So, are you interested in taking a look at the example below and thinking about what this in base class B represents?
public class B { public B() { System.out.println(this.getClass().getSimpleName()); System.out.println(((A) this).a); } } public class A extends B { public int a = 100; public A() { a = 200; } public static void main(String[] args) { new A(); } }
This example was originally intended to understand how a class is initialized when java has a inheritance structure, but the constructor in class B is quite special. : The SimpleName output by this in the constructor in class B is A. Usually when we encounter the situation, the SimpleName output by this in class B should be B, but here it is A? Why?
In the process of talking about this above, we have already touched on this. When calling the java method to create a stack frame, the jvm will secretly pass a current instance. Therefore, when we execute the constructor of A, the constructor of parent class B will be called by default. When calling the constructor of parent class B, the current instance passed in secretly is the instance of A - because it is B is called in the constructor of A, so this here represents A instead.
The above is the detailed content of Detailed explanation of Java keyword this (power node arrangement). For more information, please follow other related articles on the PHP Chinese website!