1

I've just started learning Java and it's great. One thing I need to understand that in a class we can access instance variable in two ways:

class Box {

    // Instance variables
    private int width;
    private int height;
    private int depth;

    // First way
    public void set_volume(int a, int b, int c) {
        this.width = a;
        this.height = b;
        this.depth = c;
    }

    // Second way
    public void set_volume_v2(int a, int b, int c) {
        width = a;
        height = b;
        depth = c;
    }

}

Here, Instance variable is accessible without this keyword and with it. So what's the best way? OR What's the difference between them?

Yousuf Memon
  • 4,638
  • 12
  • 41
  • 57
  • 3
    Both are ok. You typically use the `this` keyword when the field name is hidden by a argument. – Bart Aug 01 '14 at 11:49
  • 1
    In your case both are same, as there is no ambiguity in resolving local and instance variables as their name differs ! Sometimes it is better to use `this` inside a lengthy method just to be more clear in identifying instance variables and local variables. – AllTooSir Aug 01 '14 at 11:51
  • Search before you post : http://stackoverflow.com/questions/725770/should-the-java-this-keyword-be-used-when-it-is-optional , http://stackoverflow.com/questions/516291/the-use-of-this-in-java, http://programmers.stackexchange.com/questions/113430/what-is-the-accepted-style-for-using-the-this-keyword-in-java – willowherb Aug 01 '14 at 11:55
  • @Shiva The 3 links don't answer it. By the way nice attempt. – Yousuf Memon Aug 01 '14 at 12:07
  • 1
    One comment for put-on-holders that the 3 answers I got are all same they don't differ by opinion. So, this question no-way looks to me an opinion based question. – Yousuf Memon Aug 01 '14 at 12:09
  • "what's the best way?" is a question asking for opinions. The other question "What's the difference between them?" is perfectly answerable though. I would recommend editing out the first question. – Sumurai8 Aug 01 '14 at 12:19

3 Answers3

5

Using this will allow you to make sure you are referencing the instance variable instead of the argument, should they share the same name.

This is often thought as a best practice in instance methods and constructors.

Otherwise your two methods are equivalent.

Mena
  • 47,782
  • 11
  • 87
  • 106
3

Best way is to always use this

 this.width = a;

So that we never confuse between the argument names and instance variables.

One small example to confuse is

  public void set_volume_v2(int width) {
        width = width;
    }

So when you write this.width we are making it clear that it's instance variable.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    @downvoter can you please comment about my mistake ? – Suresh Atta Aug 01 '14 at 11:56
  • That's why all parameters should be final. Your example would have a compilation error if the width parameter was final. Not the downvoter btw. – maba Aug 01 '14 at 12:23
2

Instance variable should be access with this keyword it eleminates the confusion of same name of local veriable. This would ignore below kind of problem -

public void set_volume_v2(int width, int height, int depth) {
    width = width;
    height = height;
    depth = depth;
}

Here for more preference local veriable would be used.

public void set_volume_v2(int width, int height, int depth) {
    this.width = width;
    this.height = height;
    this.depth = depth;
}

And this.width make sure that is instance variable and width is local variable.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • You will only need the `this` keyword in that particular case. It's perfectly fine to reference a field without it. – Bart Aug 01 '14 at 11:54
  • 2
    @Bart That is the reason. We should always habituate to use `this` , so you need not to remember any cases :) – Suresh Atta Aug 01 '14 at 11:55