The keyword This is the reference name that points to the calling object itself;
Whenever an object is created, the Java virtual Opportunity assigns a pointer to the object that refers to itself, and the name of the pointer is this;
Notes on this in the Java Programming Mind:
The This keyword can only be used inside a method, representing a reference to the "object that invokes the method";
Another method that calls the same class within a method does not have to use this, which is called directly, and of course you can add;
We do not need to add this, because the compiler can help us automatically added;
Example : The following method in Testthisone plus this and no add is the same;
Source:
Public class thistest{
Public static void main (string[] args) {
Test Test = new test ();
Test. Testthisone ();
}
}
class test{
Public void Testthisone () {
Testthistwo (10);
This . Testthistwo (+);
}
Public void testthistwo (int i) {
System. out. println (i);
}
}
Operation Result:
You can use return this to return a reference to this object;
Others blog Summary
it says in this blog : This can only be used in static methods and static code blocks in a class that are not static, This;this can only be associated with a particular object, not with a class, and different objects of the same class have different this;
Try: Try to use this in the static method, as shown in the results, if you use this in Testthisone, the following mutation error will be reported, if you do not use this, the operation is normal;
Source:
Public class thistest{
Public static void main (string[] args) {
Test Test = new test ();
Test. Testthisone();
}
}
class test{
Public static void Testthisone () {
This . Testthistwo (+);
}
Public static void testthistwo (int i) {
System. out. println (i);
}
}
Operation Result:
This usage
Usage One: The Hidden data field of the reference Class (hidden), the member variable is masked when the function parameter or the local variable in the function has the same name as the member variable;
Example : by outputting this.i, it can be seen that this function is to call the object's variable I, because it is a member variable inside the class, the basic data type is not initialized when it is automatically initialized, the int default is 0, in the Myprint method there is a variable i, but here because this point to the object itself is the reference name, so here thi S.I is the member variable I of the class, not the I defined in the method;
Source:
Public class thistest{
Public static void main (string[] args) {
Test Test = new test ();
Test. Myprint (10);
}
}
class test{
Private static int i;
Public void myprint (int i) {
System. out. println (i);
System. out. println (this. I);
}
}
Operation Result:
Usage Two: Let the construction method call another construction method of the same class;
For example: The new Test () calls the parameterless constructor as shown below, but calls the other parameter constructor with this (the argument list) in the parameterless constructor
Source:
Public class thistest{
Public static void Main (string[] args) {
test test = new test ();
}
}
class test{
Public Test () {
This (ten);
}
Public Test (int i) {
System.out.println (i);
}
}
Operation Result:
Summarize:
About the use of this is probably the case, other such as why the static can not use this, it is not clear, and so many more to summarize it together;
Java keyword this