9

For example, in the following code:

private int id;

public void setID(int ID) {
    this.id = ID;
}

public void getID() {
    return id;
}

Why don't we say return this.id in the getter function or conversely say id = ID in the setter function? Also is this actually necessary? I mean, aren't the functions called through an object, say obj.setid(1) or obj.getid()? Will it work differently if I don't use the this keyword?

Shreyas Yakhob
  • 321
  • 1
  • 3
  • 15
  • 6
    Often in setters, you have a method argument with the same name as the instance variable that you are setting. In the code you posted, you called one `ID` and one `id`, but you could have called them both `id`. Then `this.` would be required to distinguish the instance variable from the method argument. If you call the two variables different things, you don't need `this.` – khelwood May 08 '17 at 08:48
  • 1
    Here in this code, You don't need to use `this` keyword. – Sanket Makani May 08 '17 at 08:48
  • 1
    `this` is only required if a local variable name hides a field. Since your local variable in the setter (`ID`) is not the same as your field (`id`), `this` is not required at all. But variable names **shouldn't start with an uppercase letter** according to the Java Naming Convention, so change your variable quickly. – MC Emperor May 08 '17 at 08:52

4 Answers4

16

You need to use this when the variable names are the same. It is to distinguish between them.

public void setID(int id) {
    this.id = id;
}

The following will still work when this is removed. It is because their names are not the same.

public void setID(int ID) {
    id = ID;
}
Sky
  • 1,435
  • 1
  • 15
  • 24
9

NO, we use it in both just in case the name of your attributes is same, your example not provide this case, so consider you have this :

private int id;

public void setID(int id) {
//                    ^^---------This instead of ID
    this.id = id;//<<------------So to make a difference between the attribute of method 
                               //and the id declared in the class we use this
}

public void getID() {
    return this.id;//<<<---------you can use this.id also here it is not forbidden
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
4

We usually do this to avoid DataShadowing Because if you do below thing in setter (assuming your method parameter name is id instead of ID

public void setID(int id) {
   id = id; // You are reassigning the value to the same variable
}

whereas if you want to set the value in Instance level variable then you must use this keyword (which represent the current object)

Note: In your case this is not required as local and the instance level variable name is different.

Hope this clears your confusion

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
2

When you have define a class and an object of that class,you need this to initialize in setter if the passed argument name and the attribute name of the object are same.

public void setID(int id) { this.id = id; }

Basically this refers to the object through which the call is made.

bigbounty
  • 16,526
  • 5
  • 37
  • 65