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?
The second important use of
this
(beside hiding with a local variable as many answers already say) is when accessing an outer instance from a nested non-static class:Unless you have overlapping variable names, its really just for clarity when you're reading the code.
There are a lot of good answers, but there is another very minor reason to put
this
everywhere. If you have tried opening your source codes from a normal text editor (e.g. notepad etc), usingthis
will make it a whole lot clearer to read.Imagine this:
This is very clear to read with any modern IDE, but this will be a total nightmare to read with a regular text editor.
You will struggle to find out where
foo
resides, until you use the editor's "find" function. Then you will scream atgetStringFromSomewhere()
for the same reason. Lastly, after you have forgotten whats
is, thatbar = s
is going to give you the final blow.Compare it to this:
foo
is a variable declared in outer classHello
.getStringFromSomewhere()
is a method declared in outer class as well.bar
belongs toWorld
class, ands
is a local variable declared in that method.Of course, whenever you design something, you create rules. So while designing your API or project, if your rules include "if someone opens all these source codes with a notepad, he or she should shoot him/herself in the head," then you are totally fine not to do this.
Google turned up a page on the Sun site that discusses this a bit.
You're right about the variable;
this
can indeed be used to differentiate a method variable from a class field.However, I really hate that convention. Giving two different variables literally identical names is a recipe for bugs. I much prefer something along the lines of:
Same results, but with no chance of a bug where you accidentally refer to
x
when you really meant to be referring tox
instead.As to using it with a method, you're right about the effects; you'll get the same results with or without it. Can you use it? Sure. Should you use it? Up to you, but given that I personally think it's pointless verbosity that doesn't add any clarity (unless the code is crammed full of static import statements), I'm not inclined to use it myself.
To make sure that the current object's members are used. Cases where thread safety is a concern, some applications may change the wrong objects member values, for that reason this should be applied to the member so that the correct object member value is used.
If your object is not concerned with thread safety then there is no reason to specify which object member's value is used.