[Javase Study Notes]-7.6 principle of this keyword
In this section, a keyword is the this keyword.
Let's take an example:
class Person{private String name;private int age;Person(String n,int a){name = n;age = a;}public void speak(){System.out.println(name+":"+age);}}
class ThisTest {public static void main(String[] args) {Person kobe = new Person("KOBE",37);kobe.speak();}}
We should be familiar with this example. We have used this example in the previous sections. Let's take a look at the result:
Obviously, the constructor initializes the object kobe.
However, we found that although the result is what we want, we can look at this constructor separately. From the perspective of readability, we do not know what the function has passed in, we can say that we do not know anything, and the readability is too bad. Then we will transform this constructor:
class Person{private String name;private int age;Person(String name,int age){name = name;age = age;}public void speak(){System.out.println(name+":"+age);}}
Alas, isn't that clear? We can see at a glance that this constructor is going to tell us the name and age of the initialization object. Let's see the result:
Well ??? What is the situation? What about KOBE?
We talked about the memory loading process of the constructor in section 7.4, but we didn't mention this situation here. In this case, we can say that the member variables and local variables have the same name, in this case, both the stack memory and heap memory will have the variable name and age, and the called constructor will automatically search for these two variables in the stack memory, so the system will do a very interesting thing, that is, assigning the name in the stack memory to itself, and the value of the object name and age is actually in the heap memory, so the result is what we just saw.
So how can we solve this problem?
Java gives us a solution, that is, to use the keyword "this" to distinguish between member variables and local variables. Let's make another transformation:
class Person{private String name;private int age;Person(String name,int age){this.name = name;this.age = age;}public void speak(){System.out.println(name+":"+age);}}
Result:
Good. Comrade KOBE is back.
So we can say that we can use this to distinguish between member variables and local variables.
So what exactly does this mean? In java, this represents an object. We also want to know which object this represents? The java language also indicates the current object.
The term "this" is defined as follows: this is the reference of the object where the function is located. To put it simply, this indicates the reference of this class object.
We define it by ourselves in the popular language: Which object calls the function of this, and this indicates the object, that is, this is the reference of this object.
For example, if kobe in the above example calls the constructor Person (String name, int age), then we can say that this can represent the kobe object.
Then let's analyze the embodiment of this in the memory. We continue the 7.4 process with only a small change.
1. The main method is in the stack memory. The main method has a Person class variable kobe;
2. Create a Person object in new and create a space in heap memory (if the address is 0x0045). There are two member variables: name and age;
3. initialize two member variables of the object. At this time, the constructor Person (String n, int a) is automatically called );
4. the constructor Person (String name, int age) is added to the stack memory. The parameter name = "KOBE" and age = 0 are also loaded into the stack. At this time, the system automatically loads an object reference for the stack memory, that is, this, and assigns the heap memory address of kobe to this;
5. then. name and this. the age is initialized to the name and age in the stack memory, which makes it clear that this. name and this. age, we can understand that this refers to the member variable of the object in the heap memory. At this time, the object initialization is complete;
6. Assign the address 0x0045 to the instance variable kobe in the main method;
7. The constructor Person (String name, int age) goes out of the stack and releases the parameter name, age, and this references;
8. run kobe. when the speak () Statement calls the speak () method in the Person class, the speak method is pushed to the stack. At this time, the system also loads a this reference for the speak method, point to the object address in heap memory (0x0045 );
9. Execute the print statement, jump out of the speak method, exit the stack using the speak method, and release this reference;
10. jump out of the main method, the main method goes out of the stack, and the program running ends.
Through the above process analysis, we can simply conclude that this keyword is used when the object that calls the function needs to be used in the function.
To better understand this conclusion, we can write the above example as follows:
class Person{private String name;private int age;Person(String name,int age){this.name = name;this.age = age;}public void speak(){System.out.println(this.name+":"+this.age);}}
All member variables in this class method are written as this. member variables in the standard format.
Why is the result correct without this in the first example?
Obviously, this can be omitted when the member variables and local variables are not duplicated. However, please note that this is not omitted.