Because this keyword is often seen in Java programs, the author simply summed up its usage, and everyone to learn from each other. In general this is used in several places:
(1) When the local variable and the member variable have the same name, you need to use this to distinguish
such as: public class text (int number) {
This.number=number; Write number=number directly; get 0, because it doesn't make sense to assign yourself to yourself.
System.out.println ("number=" +this.number);
}
(2) This () represents the invocation of another constructor, which calls that function, depending on the type and number of arguments given when this () is called.
Note: the A.this () call can only appear in the first row of the constructor;
B. Constructors cannot be called within any function other than the constructor;
C. Only one constructor can be called within a constructor;
Readers can think about why these considerations occur.
(3) This represents a reference to the current object
such as: public class Thistest () {
pricate int j=0;
for (int i=0;i<3;i++) {
this.j=j++;
}
return this;
}
(4) This cannot be used in the static method. Because the this keyword points to the current object, and the static adornment method belongs to the class, the caller of the class may be a class rather than an object, causing this to fail to point to the appropriate object.
I hope that the author's summary can bring some help to everyone, because the author's level of limited also hope that the great God all my mistakes to point out. For everyone's problems can also be at the end of the message, we can exchange learning together.
This keyword parsing in Java