This keyword of java, java keyword
Scope of use of this
1. The this keyword used in the method definition of the class indicates the reference to call the method object.
2. When you must specify the object of the current method, use the keyword "this.
3. Sometimes this can be used to handle the case where the member variables and parameters in the method are duplicated.
4. this can be seen as a variable whose value is a reference to the current object.
Note: this usually appears in the method, when the method is not called. I don't know the specific object that this points.
When an object calls a method with this, this points to the object that calls this method.
Program code:
public class TestThis{ private int i; public TestThis(int i){ this.i = i; } private TestThis increment(){ i += 1; return this; } public static void main (String[] args){ TestThis mTestThis = new TestThis(100); System.out.println(mTestThis.increment().increment().i); }}
Output result: 102
This keyword for java
This in java is everywhere and used in many ways. The following points are available:
1. this refers to the current object itself.
This reference should be added when the variables or functions of the object must be explicitly specified in a class. In the following example:
Public class Hello {
String s = "Hello ";
Public Hello (String s)
{
System. out. println ("s =" + s );
System. out. println ("1-> this. s =" + this. s );
This. s = s;
System. out. println ("2-> this. s =" + this. s );
}
Public static void main (String [] args ){
Hello x = "new" Hello ("HelloWorld! ");
}
}
Running result:
S = HelloWorld!
1-> this. s = Hello
2-> this. s = HelloWorld!
In this example, In the constructor Hello, the parameter s has the same name as the variable s in the Hello class. In this case, if the parameter s is operated directly, the parameter s is operated. To operate the member variable s of the Hello class, use this to reference it. The first line of the running result is to directly print the parameter s passed in the constructor; the second line is to print the member variable s; the third line is to first print the parameter s value assigned to the member variable s, so the result is HelloWorld!
2. Pass this as a parameter
When you want to pass yourself as a parameter to another object, you can also use this. For example:
Public class {
Public (){
New B (this). print ();
}
Public void print (){
System. out. println ("Hello from! ");
}
}
Public class B {
A;
Public B (A ){
This. a =;
}
Public void print (){
A. print ();
System. out. println ("Hello from B! ");
}
}
Running result:
Hello from!
Hello from B!
In this example, the constructor of object A uses new B (this) to pass object A as A parameter to the constructor of object B.
3. Pay attention to this in the anonymous class and internal class.
Sometimes, we use some internal and anonymous classes, such as event processing. When running? When Xing uploads his, this refers to the anonymous class or internal class itself. If we want to use external class methods and variables, we should add the Class Name of the external class. For example:
Public class {
Int I = 1;
Public (){
Thread thread = new Thread (){
Public void run (){
For (;;){
A. this. run ();
Try {
Sleep (1000 );
} Catch (InterruptedException ie ){}
}
}
}; // Note the following;
Thread. start () ...... the remaining full text>
This keyword usage in java
This is used in many ways.
The usage depends on the specific environment to determine, and requires the upper and lower code to understand
This can refer to a method or variable in the main method within the method.
This application is flexible ....
Because I am also a beginner, I am not very thorough, but it is probably like that.