1, What is this keyword
this is a keyword in java;
This can be a reference or a variable in java. When it is a variable, the memory address saved in the variable points to itself, and this is stored in the JVM heap memory java object
2, What is the function of this keyword
1. The first function of this keyword is to pass the value of the local variable to the instance variable when it is assigned when the local variable and the member variable have the same name when the constructor passes the parameter, and add this [syntax format: this.];
2. This keyword can appear in the instance method. This refers to the object currently executing this action (this represents the current object);
Detailed explanation of this key1 public class Test19 { 2 3 // attribute 4 String name; 5 6 String interest; 7 8 public static void main(String[] args) { 9 10 // create object 11 Test19 t = new Test19("Cheng Jian","play games" ); 12 13 // this.Test(); 14 // Use this Error; 15 // because this It cannot be used in static methods, so it can only be used through "reference" in static methods . " Method of calling 16 17 t.Test(); 18 19 } 20 21 22 // Create a construction method 23 public Test19() { 24 25 } 26 27 28 // Parametric construction method 29 public Test19(String name, String interest) { // The parameter name here is the same as the member variable name in the property, but the syntax is correct 30 31 this.name = name; 32 // In the construction method, it is wrong to assign member variables in this way name stay JVM It seems that none of them is a member variable 33 // So this time this The function of a keyword is to specify itself and its own properties in the object 34 this.interest = interest; 35 // this Keyword to be used when pointing to instance variables“ this. " Method to call properties 36 37 } 38 39 // Create a common method 40 public void Test() { 41 System.out.println("I was this Pointing to"); 42 } 43 44 public void Test_Test() { 45 System.out.println("I was this Pointing to"); 46 47 Test(); 48 // Amazing things happen. In another way, another method can be invoked without reference.. Can be called directly by class name 49 // The reason is that when the method is called, there is a this. Calling the method, but this It can be omitted. 50 this.Test(); 51 } 52 }