The example in this article describes the usage of this keyword in java and is shared with everyone for your reference. The specific analysis is as follows:
1. Scope of use of this
1. The this keyword used in the class method definition represents the reference of the object that calls the method.
2. When it is necessary to indicate who is currently using the method, use the keyword this.
3. Sometimes this can be used to deal with the situation where member variables and parameters in methods have the same name.
4. This can be regarded as a variable, and its value is a reference to the current object.
Note: this generally appears in a method when the method is not called. I don't know which specific object this points to.
When an object calls a method with this, this points to the object that called this method.
2. The program code is as follows:
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); }}
The output is as shown below:
I hope this article will be helpful to everyone’s Java programming.