You don't really need to write the "this" keyword in Java. But is it better to do so anyway? Does it make sense to homogenise your style, i.e. if you use "this" once, you use it every time it's implied? Or is there a place where you would always use it and others where you never use it?
-
4This is something that always puzzled me. As a general rule it's avoided as much as you can, but then a lot of people call instance attributes/members with names that start with `m` to remind you that it is an instance member... why not just leave out the `m` and do `this.theMemberName`? – Bakuriu Feb 18 '13 at 09:09
-
@Bakuriu blame the Hungarians! http://en.wikipedia.org/wiki/Hungarian_notation I believe it's the whole "hackers are lazy" motif that runs through history. since m_theMemberName is shorter than this.theMemberName, they tend to prefer the former. – Soyuz Feb 18 '13 at 09:13
-
I like to use `this` in methods that take Objects of the same class as a parameter, for example `equals()` and `compareTo()` – Adrian Pronk Feb 18 '13 at 10:06
-
I'm just looking at some legacy code with twelve unnecessary and inappropriate uses of *this.* in one file. Already I am worried about readability and code quality. Don't use *this.* except in the few cases where it is warranted!! – vikingsteve Aug 08 '13 at 07:50
4 Answers
The general consensus is that you should use this
only when it is necessary and not at any other time.
private String param;
public Construct(String param) {
// Usually the only place you need to use this.
this.param = param;
}
// A less common use of this
public Function() {
synchronized(this) {
// Some synchronized stuff.
}
}

- 64,482
- 16
- 119
- 213
As a rule I tend not to use it - if you can reduce redundant code then all the better.
There are however three places that I can think of where the this keyword cant be avoided:
Constructors (delegating to another constructor in the same class)
public MyClass() { this("Default Parameter");
Synchronizing on the current object
synchronized(this) {
Passing the current object to another class
public void methodOne() { anotherClass.doSomething(this);
You sometimes need it in constructors where the field name is the same as the parameter, but this isnt really mandatory as you could simply rename the paramter:
public MyClass(String data) {
this.data = data
Other than these I cant think of too many other scenarios where I'd use the this
keyword. I have seen it overused (on every method and field reference) and this can make the code very hard to read.
Use it only when you have to, or when you believe that it enhances code readability.

- 7,033
- 2
- 29
- 33
As a general rule you should avoid redundant syntax wherever it may arise. You will read lots of opinion to the contrary, mostly referring to a thoroughly mythical programmer who doesn't know about member variables, doesn't know what parentheses are for, and doesn't remember the rules of operator precedence he was taught in third grade. I've never met him in 40 years. That isn't enough to justify disfiguring your code for him on the assumption that he will (a) not understand it and (b) therefore break it. I've never seen that happen at all.
What I have seen is code produced by such a person. That's not a reason to dumb down your own code for him. The occasions on which someone has actually gone as far as to incorrectly rewrite a piece of code of mine are exactly two: once in 1979, where someone refactored a grammar to remove the operator precedence, which was dumb, and he shouldn't have done it, and another time in about 1992, but in both cases there is no way I could have written the grammar or the code that would have prevented it.

- 305,947
- 44
- 307
- 483
There are some places in code, where you couldn't skip this
keyword, e.g. setters:
public void setValue(String value) {
this.value = value;
}
But if its possible it's better to skip:
public String getValue() {
return value;
}

- 8,099
- 4
- 46
- 73
-
Well, actually you could simply use an other name for the parameter since in Java there aren't keyword arguments and thus this wouldn't change the API. – Bakuriu Feb 18 '13 at 09:11