1

I have a question about the this operator in Java. In the case where a programmer would write a code like this:

private int counter;

public Incrementor(int counter){
this.counter = counter;
}

Would it be better to avoid shadowing effect and go with this:

private int counter;

public Incrementor(int startValue){
counter = startValue;
}

Wouldn't the this operator make the this operator obsolete in future programming execrises?

4
  • 18
    You've asked 13 questions and you didn't accept any!
    – Eng.Fouad
    Oct 23, 2011 at 21:16
  • 7
    this is not an operator.
    – K-ballo
    Oct 23, 2011 at 21:16
  • You might still have to write code like if (someObject==this)..., or refer to the this of an outer class from an inner class.
    – Vlad
    Oct 23, 2011 at 21:18
  • 1
    Question is nonsensical. How can 'the this operator make the this operator obsolete'?
    – user207421
    Oct 23, 2011 at 21:30

9 Answers 9

5

The "this" keyword is very useful. Your example is clear, but imagine the situation in which you have to pass a self reference to another method of an external object.

4

this is a reference to the current object. In your example this.counter means the counter variable defined in the class and the method argument counter is a local variable for the constructor.

The main idea here is to explicitly mention that you are using the variable of the current object. So it improves readability and understandability of your code.

Also you should know that this is not an operator ... it's a reference to the current object.

4

this is used in more scenarios:

  • it allows differentiating local variables from fields
  • it allows passing the object as argument to methods foo.add(this)
  • it allows the current object to be referred from inner classes: Outerclass.this
  • it is used to invoke other constructors of the same class: this()
3

It's a matter of preference, but most programmers prefer the convenience of using the same name. Is it difficult to remember to refer to the class variables with this ?

1
  • 1
    Things that most programmers prefer aren't necessarily great practices. Oct 23, 2011 at 21:21
3

There is no shadowing in your first example. this is a very clear way in order to refer to class variables.

3

The way I see it, this is simply a matter of good programming practices when you are doing it for class variables.

A code like:

private int counter;

public Incrementor(int counter){
this.counter = counter;
}

vs.

private int counter;

public Incrementor(int startValue){
counter = startValue;
}

Extrapolate it to 10 lines, and the first one is much more readable. Moreover, it also makes refactoring code easier.

Otherwise, 'this' has a special significance. The 'this' keyword refers to the current object inside a class and falls in line with object-oriented principles. It can be extremely helpful and meaningful in case of getters and setters.

Added:

Once you start working with inheritance, you will notice another keyword called 'super' that can significantly differentiate between attributes that are inherited and ones that are local inside a class.

2

In small methods, it is often more clear to give arguments the same name as the members they will be assigned to. If you are working with a larger method, this could become confusing. But then you need to evaluate whether the right decision is to change the name of the argument(s) or to decompose the method into sub-functions.

2

I would use the first one. It makes the code more readable. Also you should name the method setCounter() as this also lends to readability.

Another unrelated point is to stick with the Java Coding conventions for naming methods as outlined in the Methods section here.

Edit: That wasn't a method was it. Oops!

1
  • +1 for mentioning the Java Coding Conventions. I would recommend starting with the JCC and modifying them to suit your needs if appropriate. Oct 23, 2011 at 21:23
1

Sometimes, you need to pass the object itself. That's when you use this.

4
  • 2
    not if you have an argument with the same name as the field. Oct 23, 2011 at 21:21
  • 1
    Using this to refer to a field is absolutely not redundant - the original question shows this. Even if it were redundant, it would be helpful for code clarity.
    – idbentley
    Oct 23, 2011 at 21:56
  • Alright, alright, somehow I knew I would get flak for this. Then again, explicitly marking fields with this is something I only saw in Java; not in C++, and not in C#. I wonder why. Oct 24, 2011 at 1:51
  • 1
    I was doing it in C++ twenty years ago. You don't see it so much in C++ because it has the : syntax for member initialization in constructors; which also dictates that members have different names from constructor arguments; which in turn tends to lead to a _ or m_ prefix on member variables; which in turn eliminates the requirement for disambiguation.
    – user207421
    Oct 24, 2011 at 2:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.