Well, this is not Java, but it looks great for a code picture!

JAVA — When should I use “this” keyword?

Emiliano Viotti
3 min readMar 17, 2018

It does not matter if you just start coding with Java or you are a senior developer with tons of lines of code in your backpack, surely you ever wondered at some point in your life:

When should I use "this" keyword and when should I not?

We know that this refers to a current object. But I do really know when I should use it?. For example, will be there any difference if I use x instead of this.x in some of the methods? May be x will refer to a variable which is local for the considered method? I mean variable which is seen only in this method.

What about this.method()? Can I use it? Should I use it. If I just use method(), will it not be, by default, applied to the current object?

Well, this publication aims to clarify this issue through a list of common scenarios that I have been compiling over time from Stack Overflow questions and other technical articles.

Case 1

Using this to disambiguate variable references. In Java the most common way to define a setter method, is to pass an argument with the same name as the private member variable we are attempting to set. We then assign the argument x to this.x. This makes it pretty clear that you are assigning the value of the parameter “name” to the instance variable “name”.

Case 2

Using this as an argument passed to another object. Sometimes we want to pass the instance of the current object as an argument of another object method. This is used a lot for example when you have a bidirectional relationship between two classes. Supose object a which has an association with object b then we implement a method a.addBInstance(b), that internally call method b.addAInstance(this).

Case 3

Using this to call alternative class constructor methods. When you have multiple constructors for a single class, you can use this(arg0, arg1, ...) to call another constructor of your choosing, but remember always to do so in the first line of your constructor (if you are asking yourself why in first line check this).

Case 4

Another important use of this is when you attempt to access an outer instance from a nested non-static class.

Case 5

Using this in builder pattern for class construction. The Builder design pattern is a creational design pattern intended to separate the construction of a complex object from its representation and It is one of the well known GoF design patterns.

Extra case

Using this to emphasize the fact that an instance variable is being referenced. I have also seen this case in my life as a programmer, but I think that in most cases it should not be necessary since there are not need for disambiguation, on the contrary, if that is your case, I would ask myself if that is the main reason why my code does not seen clear.

Conclusion

Now that you know some of the most common scenarios where you must use this keyword, you have more information to decide by yourself if you should use it or not, and improve your codes making them cleaner and safety.

Of course, it is entirely up to you to decide if you are going to use this keyword in cases where there is no need to use it at all. How ever, and this is my personal opinion, in order to make cleaner and readable codes, I recommend using this keyword only in cases where it is strictly necessary to use it.

Finally, this post was built using Java examples but you could perfectly take these ideas and apply them to other programming languages such us Python, for example, where the question becomes when should I use self keyword and when should I not, or the code language you prefer.

--

--

Emiliano Viotti

Machine Learning Director at IDATHA.com — AI Builder — CV and NLP practitioner — Hungry reader and stories writer — Former professor at Fing UdelaR.