Java ------ this keyword

Keywords: Java jvm Attribute

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);

 1 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 }
Detailed explanation of this key

3. This keyword can be used in the construction method to call other construction methods through the current construction method [syntax format: this()];

 1 public class Test20 {
 2 
 3     // attribute 
 4     String year;
 5     String month;
 6     String day;
 7     
 8     public static void main(String[] args) {
 9         
10         Test20 t = new Test20();
11         new Test20("2020","4","9");
12         
13     }
14 
15     // Create a construction method with parameters
16     public Test20(String year, String month, String day) {
17     
18         this.year = year;
19         this.month = month;
20         this.day = day;
21         System.out.println(year + "-" + month + "-" + day);
22     }
23     
24     public Test20() {
25         // this() To call another constructor in one constructor
26         this("1970","1","1");
27         
28         // But there's a point to keep in mind: this() It can only be placed at the beginning of the sentence in the method body. If there is any code before it, then  this() It's not going to work
29     }
30 }
Detailed rules for the use of this()

 

3, The difference between this method and static method

This keyword can call properties and other methods in ordinary methods, but this can be omitted in the calling process;

this keyword cannot be used in static methods, neither calling other methods nor calling properties;

 1 public class Test21 {
 2 
 3     public static void main(String[] args) {
 4         
 5         // create object
 6         Test21 t = new Test21();
 7     
 8         // Calls to normal methods: References . To call
 9         t.Test_e();
10         
11         // Calls to static methods: class name. In most cases, the class name can be omitted.
12         // Test21.Test_t(); √
13         Test_t();
14     }
15     
16     
17     // Create a common method
18     public void Test_e() {        
19         
20         System.out.println("I'm the normal way");
21         
22         // Calling static methods in common methods
23         // Test21 t = new Test21();
24         // t.Test_t();
25 
26         // have access to this. Call static methods in
27         this.Test_t(); // Of course here this. It can also be omitted
28         
29     }
30     
31     // Create a static method
32     public static void Test_t() {
33     
34         System.out.println("I'm a static method");
35         
36     }
37 }

Posted by ashok_bam on Thu, 09 Apr 2020 08:16:59 -0700