1

I know that both chunks of code work but as I am trying to understand the use of "this" I would really appreciate it if you could explain me why the class Vehicle1 is 'better' than Vehicle2. Thanks a lot in advance!

public class Vehicle1 {
  private String color;

  Vehicle(String c) {
    this.setVehicle(c);
  }
  Vehicle() {
    this.setVehicle("Red");;
  }
  public void setVehicle(String c) {
      this.color = c;
  }
  public String getVehicle() {
      return color;
  }
}


public class Vehicle2 {
  private String color;

  Vehicle(String c) {
    color = c;
  }
  Vehicle() {
    color = "Red";
  }
  public String getVehicle() {
      return color;
  }
}
Ari
  • 324
  • 3
  • 13
  • https://stackoverflow.com/questions/10666276/java-this-keyword – Andrey Jun 26 '17 at 19:09
  • 5
    The second version is actually better. But that has nothing to do with the use of `this`. A constructor shouldn't call an overridable method. Why do you name setVehicle()/getVehicle() methods that set and get the color. Why not setColor()/getColor()? – JB Nizet Jun 26 '17 at 19:11
  • `this` is a reference the current Object. You can use `this` to do everything you could do with a final variable (you can't assign it), plus (as the first statement in a constructor) invoking another constructor. – Felix Jun 26 '17 at 19:34
  • `this.` as a method prefix is utterly useless. Your constructors do not chain. You have doubled semicolons. The first example calls an overridable method from a constructor. The constructors are not public. Both examples are crap. – Lew Bloch Jun 26 '17 at 21:46

3 Answers3

3

It's largely a matter of preference, but one advantage to using this is that it avoids naming collisions with more local variables. For example, if you wanted people calling your constructor to know that its argument is supposed to be a color, you might want to name it such:

  Vehicle(String color) {
    color = color;        // This doesn't set the field.
  }

The code above shows, without using this, that the field name color is now usurped by the local parameter name. Depending on your settings, you may get some compiler warnings, but it's possible to overlook that bug.

If you use this., then everything continues working with no additional changes:

  Vehicle(String color) {
    this.color = color;
  }
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
0

Vehicle 1 is better because it removes any ambiguity or confusion. In vehicle 2, if you have another "color" variable inside a method, then you will be dealing with that local variable, rather than the color of the car itself.

frozen
  • 2,114
  • 14
  • 33
  • This answer is wrong. There was no ambiguity in the first example, so it couldn't be reduced. You cannot have less than zero ambiguity. – Lew Bloch Jun 26 '17 at 21:47
0

We use this if local variable, or method argument may shadow class property. It points that this particular reference will be found within class properties.

Example:

The main difference can be seen when method/local properties are the same as instance properties:

This code does exactly as we expect, assign value to color property:

public void setVehicle(String c) {
   this.color = c;
}

So here this is not necssary, because method argument name does not shadow property color.

But if we would use same argument name as property name:

public void setVehicle(String color) {
   color = color;
}

then it would mean assign method argument color to itself, which has no point.

So if we want to set a new property value, we need to use this, in order to tell the compiler that color is a instance property, not local a variable.

Beri
  • 11,470
  • 4
  • 35
  • 57