0

i havent used it in a bit and to my understanding, if a had a class member called speed, and a parameter in a setter method called speed, if i used the keyword "this" it would be referring to the parameter, so the method should be written

public void setSpeed(float speed)
{
  speed = this.speed;
}

however, in the documentation for it, the example shows it the other way (which i know ive seen and used several times in the past)

public void setSpeed(float speed)
{
  this.speed = speed;
 }

so am i mixing up what the "this" keyword is actually referring to or is this just special syntax involved with using it for some reason?

ViceVersa666444
  • 861
  • 1
  • 6
  • 4
  • 1
    _"if i used the keyword "this" it would be referring to the parameter"_ - Nope. "The **this** keyword refers to the current instance of the class..." https://msdn.microsoft.com/en-us/library/dk1507sz.aspx – Ulric Sep 30 '15 at 16:21
  • 1
    `speed = this.speed;` will assign the field `speed` into local variable inside method. but `this.speed = speed;` will assign local variable from method to field `speed` inside current instance. – M.kazem Akhgary Sep 30 '15 at 16:27
  • thank you for clearing that up for me – ViceVersa666444 Sep 30 '15 at 16:49

0 Answers0