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?

6
  • 3
    Both are ok. You typically use the this keyword when the field name is hidden by a argument. Commented Aug 1, 2014 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. Commented Aug 1, 2014 at 11:51
  • Search before you post : stackoverflow.com/questions/725770/… , stackoverflow.com/questions/516291/the-use-of-this-in-java, programmers.stackexchange.com/questions/113430/… Commented Aug 1, 2014 at 11:55
  • @Shiva The 3 links don't answer it. By the way nice attempt. Commented Aug 1, 2014 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. Commented Aug 1, 2014 at 12:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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.

2 Comments

You will only need the this keyword in that particular case. It's perfectly fine to reference a field without it.
@Bart That is the reason. We should always habituate to use this , so you need not to remember any cases :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.