this
is used to refrences variables in the class. For example
public class MyClass {
private Integer i;
public MyClass(Integer i) {
this.i = i;
}
}
In this code we are assigning the parameter i to the field i in the class. If you did not have the this then the parameter i would be assigned to itself. Usually you have different parameter names so you do not need this. For example
public class MyClass {
private Integer i;
public MyClass(Integer j) {
this.i = j;
//i = j; //this line does the same thing as the line above.
}
}
In the above example you do not need this
infront of the i
In summary you can use this to precede all your class fields. Most of the time you do not need to but if there is any sort of name shadowing then you can use this
to explicitly say you are referring to a field.
You can also use this
to refer to an object. It is used when you are dealing with inner classes and want to reference the outer class.