2

I've tried to look around but it's hard searching for "this." But I just can't seem to grasp the difference between these two

public class x{
int y = 0;
int z = 0;

x(int y, int z){
    y = y;
    z = z
    }
}

and

public class x{
int y = 0;
int z = 0;

x(int y, int z){
    this.y = y;
    this.z = z;
   }
}
user3605508
  • 303
  • 2
  • 12

2 Answers2

12

When you write

y = y;
z = z;

You're just assigning the local variables to themselves, and not touching the instance variables at all. Since y refers to a local variable, you have to write this.y to refer to the instance variable that you want to assign.

You can help to catch mistakes like this by making your variables final if they aren't meant to be modified. For example:

x(final int y, final int z) {
    this.y = y;
    this.z = z;
}

If you take away the this. prefixes, you'll get a compile error because the local variables y and z can't be changed.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
  • but I could easily have named the local variables a and b instead; is this the only reason we use the "this" keyword? – user3605508 May 14 '14 at 00:53
  • 1
    @user3605508 Yes, and even where technically unambiguous, for readability. – nanofarad May 14 '14 at 00:54
  • Yes, and some people use a naming convention to prevent instance variable names from conflicting with local variables, such as adding a prefix `m_` or a suffix `_`. (For example: `y_ = y;`) – Wyzard May 14 '14 at 00:54
  • @user3605508 You're right, you could avoid `this` by doing so. But doing it using the `this.` is a pretty common idiom, and people will understand what you're doing, and you don't have to try to come up with different names which often just makes things more confusing to a reader. – ajb May 14 '14 at 00:55
  • @user3605508 what you say is right, but also by using `this` allows for self documenting code. It is obvious (without comments) that you are assigning to a instance field – Scary Wombat May 14 '14 at 00:56
  • P.S. I think the `_` suffix is out of fashion. I've seen it only in older books. (However, Android naming conventions do require private instance variables to start with `m`, so if you follow that convention, you might not use the `this` idiom.) – ajb May 14 '14 at 00:59
1

In Java, the this keyword represents the current instantiation of the class. So when you create an object:

x obj = new x();

x has two "instance" (not local) variables y and z.

If your class also has methods that contain local variables of the same name, then how can the Java Runtime environment (the computer) know which y and z you are referring to? For example:

x(int y, int z){
    y = y; //Both z are just the local variables 
           //of this method and don't change the class.
    z = z; //Both z are just the local variables and dont affect class object
}

But if you do the following, then you change the objects y value but not its z value:

x(int y, int z){
    this.y = 50; //call this function sets the objects y value to 50
    z = z; //But nothing happens to the objects z value because the
           //Java Runtime enviornment sees the z and finds its nearest
           //scope which is the method its defined in and thus the local
           //variable gets set to itself and then deleted after the method
           //is called and the object's z value is not changed.
}
cgnorthcutt
  • 3,890
  • 34
  • 41