Explaining the “this” keyword

Continuing my last post, that user from the NLSC forums said he didn’t understand the “this” keyword the way it was explained through the video tutorials. Although I’m 100% sure there’s many people out there, way more experienced than I am, explaining this particular keyword, I thought I’d write an answer of my own. Why? Well, if I say I want to be a teacher, I should be able to explain any programming concept that comes up in words that a person that’s beginning programming would understand. (And if I don’t know a concept, I should not be afraid to say so, read up on it, and come up with a good explanation. A teacher that admits he/she doesn’t know everything is way more useful to their students and their approach to programming, than one that makes up bullshit on the spot to cover-up what he/she doesn’t know, and ends up filling their brains with false knowledge.)

So, here’s my explanation on the very first things one should know about the “this” keyword.

Classes have properties and methods. So, for example, a Car class can have the following properties: Make, Model, Year, Horsepower, Engine Capacity, etc. So, to create a car, you either instantiate it and then set its properties one by one, or you instantiate it with a constructor that allows you to put some information right in the call.

So, in C# (which is really similar to Java) the constructor

class Car
{
  public string Make;
  public string Model;

  public Car()
  {
  }
}

can be called with

Car mycar = new Car()

This constructor doesn’t have any parameters, so you just call it with empty parentheses. Then, if you want to set the properties of the car, you do

mycar.Make = "Volvo";
mycar.Model = "X70";

etc.

But, if every car needs to have a make and model when its created, or if you just want to have more than one way to create a car, you do the following contructor:

public Car(string Make, string Model)
{
...
}

However, here, Make and Model are just two named parameters that you pass on when you create a car like so

Car mycar = new Car("Volvo", "X70")

They’re not set to the corresponding Make and Model properties of the Car class, unless you say so in the code, like so:

public Car(string Make, string Model)
{
  Make = Make;
  Model = Model;
}

See the problem with the above code? You can’t tell which Make is the one referring to the parameter that is passed to the constructor, and which one is the property of the class. To differentiate between them, you use “this”. Any property that has a “this” in front, is a class property, and not a passed on parameter. So to make the last constructor work, you’d do

public Car(string Make, string Model)
{
  this.Make = Make;
  this.Model = Model;
}

So Java now knows that when you call the constructor for Car with two strings, it will make the object’s Make property equal to the Make parameter passed to the constructor, and it will do the same for the Model.

Hope I’ve explained it.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s