You can use this if you use the same method argument argument as the field, but it can be avoided if you just don't use the same name.
Not using the same name is more common practice to avoid confusion and shadowing. Therefore, any reference to this in the setter can be replaced by a better naming standard: inParameter , for example.
public void setX(int inX) { x = inX; }
Another use of this would be to explicitly call the constructor . This is a form that cannot be replaced by a simpler naming convention:
public class Foo { private String name; public Foo() { this(""); } public Foo(String inName) { name = inName; } }
There may also be a case in which you want to return the instance you are working with. This is also what this allows:
return this;
source share