I know that this
refers to a current object. But I do not know when I really need to 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?
@William Brendel answer provided three different use cases in nice way.
Use case 1:
Offical java documentation page on this provides same use-cases.
It covers two examples :
Using this with a Field and Using this with a Constructor
Use case 2:
Other use case which has not been quoted in this post:
this
can be used to synchronize the current object in a multi-threaded application to guard critical section of data & methods.Use case 3:
Implementation of Builder pattern depends on use of
this
to return the modified object.Refer to this post
Keeping builder in separate class (fluent interface)
You only need to use
this
- and most people only use it - when there's an overlapping local variable with the same name. (Setter methods, for example.)Of course, another good reason to use
this
is that it causes intellisense to pop up in IDEs :)when there are two variables one instance variable and other local variable of the same name then we use this. to refer current executing object to avoid the conflict between the names.
The only need to use the
this.
qualifier is when another variable within the current scope shares the same name and you want to refer to the instance member (like William describes). Apart from that, there's no difference in behavior betweenx
andthis.x
.The
this
keyword is primarily used in three situations. The first and most common is in setter methods to disambiguate variable references. The second is when there is a need to pass the current class instance as an argument to a method of another object. The third is as a way to call alternate constructors from within a constructor.Case 1: Using
this
to disambiguate variable references. In Java setter methods, we commonly pass in an argument with the same name as the private member variable we are attempting to set. We then assign the argumentx
tothis.x
. This makes it 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.Case 3: Using
this
to call alternate constructors. In the comments, trinithis correctly pointed out another common use ofthis
. When you have multiple constructors for a single class, you can usethis(arg0, arg1, ...)
to call another constructor of your choosing, provided you do so in the first line of your constructor.I have also seen
this
used to emphasize the fact that an instance variable is being referenced (sans the need for disambiguation), but that is a rare case in my opinion.this
is a reference to the current object. It is used in the constructor to distinguish between the local and the current class variable which have the same name. e.g.:this
can also be use to call one constructor from another constructor. e.g.: