3

So I know how useful keyword this is in constructor, when class member and parameter uses the same name.

Do people commonly use this outside of constructor to access instance variable/member and function? Is this a good practice? Should I keep using this keyword or should I not use it?

class Test {
    private int num = 0;

    Test(int num) {
        this.num = num;
    }

    public int getNum() {
        return this.num;     // I could write num;
    }

    public void doSomething() {
        System.out.println(this.getNum());    // I could have just invoked getNum()
    }

}
J L
  • 2,157
  • 3
  • 16
  • 15
  • 6
    More or less a matter of personal preference. – Jordan Kaye Apr 02 '13 at 15:20
  • 2
    In cases like your Constructor, it's necessary. Anywhere else, it is just for readability. – Sotirios Delimanolis Apr 02 '13 at 15:21
  • It's definitely a personal preference thing. I like to do it so that it's totally unambiguous I'm referring to an instance member (because obviously I cannot be referring to a local in that case), but that's my preference. I also declare my instance variables `private final` as much as possible, so I might be an odd duck here. :-) – Platinum Azure Apr 02 '13 at 15:22
  • Please have a look at http://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class – Selim Apr 02 '13 at 15:23

5 Answers5

2

First and foremost, It's a matter of personal (or team) style. Some developers think it contributes to readability, some find it redundant and cumbersome.

Having that said, I think it's a good idea to use this because it prevents confusion between different scopes.

If this is always used, the developer who inspects a part of the code doesn't have to scroll up every time a variable is referenced just to find out what's its scope.

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
1

When working in a team the most important thing might be that all members use the same style, e.g. formatting, camelCase or mCamelCase for names and even using this keyword everywhere.

Nothing is less readable than code using different styles.

Every project you are in might have a different style, but all members should adhere to the rules.

Just be flexible.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
0

Yes, you can do this.your_field. There is no problem using the this keyword.

Ryan Amos
  • 5,422
  • 4
  • 36
  • 56
Biswajit
  • 2,434
  • 2
  • 28
  • 35
0

Try to generate getters in eclipse.. Even eclipse will use this keyword outside the constructor ;) So, yes it is commonly used and very habitual practise.

Erik Kaju
  • 3,147
  • 3
  • 19
  • 28
  • 2
    It's a good way to future-proof the code a bit, I suppose (in case someone declares a local with the same name as an instance member at the top of a method which uses an unqualified access). Maybe that's why Eclipse favors it. – Platinum Azure Apr 02 '13 at 15:23
  • 1
    Yep, i totally agree. I was trying to use this as vivid example. – Erik Kaju Apr 02 '13 at 15:25
  • It's a good example. Thank you for tolerating my musings. :-) – Platinum Azure Apr 02 '13 at 16:47
0

You only have to use it if otherwise you'd be referring to an entirely different variable/method.

In your constructor above for instance, if you hadn't used it, you'd be referring to the parameter passed to the constructor.

In your doSomething() method there was no need for the this keyword.

Henrique Ordine
  • 3,337
  • 4
  • 44
  • 70