function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
**** 

'this'

If I see something like the following code:

 

public class wrapper {

    private String string1;

    private Integer int2;

 

   public wrapper {

        this.String1 = 'abc';

        this int2 = 123';

    }

 

   public String getString1() {

        return this.String1;

    }

 

   public Integer getInt2() {

        return this.Int2;

    }

}

 

What do the 'this.'es signify and why would I need them?  Wouldn't the code work the same without them?

 

Thanks in advance.

 

 

 

 

MattLacey.ax1065MattLacey.ax1065

'this' is a reference to the current instance of a class, in the example given it doesn't really serve a purpose other than to highlight that it's referring to it's own member variables, however you can use it in scenarios where you have an argument or local variable with the same name as a member variable. For instance, in your wrapper class, you may have a setString1() method defined as so:

 

 

public void setString1(String string1){
  this.string1 = string1;
}

As you can see it allows you to differentiate between the parameter string1 and the member variable, also called string1. 

 

****

Thanks Matt.

 

So, would it be right that actually 'this.string1' could just as easily be 'thisString1' - ie the 'this.' is just part of a variable name?  I'm struggling to understand why 'this.' doesn't signify a class ... and why I can't find any documentaion on it (it's really tricky to Google for 'this').

 

Patrick

MattLacey.ax1065MattLacey.ax1065

No, like I say, it's a reference to the current instance of the class.

 

Say I've got a class called CCar, I can create a new one by calling the class constructor like so:

 

CCar myCar = new CCar();

 

The 'new' keyword indicates that I'm creating a new instance of an object, and that object is a CCar, as defined by the constructor that I'm calling. I save a reference to that instance in the variable myCar.

 

I could then do this:

 

list<CCar> listCars = new list<CCar>{myCar};

 

Which creates a new list of references to CCar instances — I store a reference to the list in listCars, and the list itself contains a reference to the CCar I created - I pass in the myCar reference and the list creates a new reference also pointing to the same object. That is, myCar and listCars[0] are both references to the same instance of CCar.

 

If I had a member variable called colour, then set it using myCar.colour = "red", I'm folliwng my reference to the instance of CCar and setting it's colour member to the value "red". If I now inspected lsitCars[0].colour, I'd find it's "red" because it's the same car, I just have to ways of accessing it.

 

"self" is a special reference to that same car, but it can only be used from within the car, so calling self.colour inside the car is the same as calling myCar.colour or listCars[0] outside of the car, they're all references (lookups if you will) to the same car. Hence to follow my previous example, calling self.colour would mean I want to access the member variable called colour belonging to the current instance of CCar, if there was a local variable called colour, I could access it just using it's name.

 

I hope that kind of makes sense, references are a bit tricky to get your head around at first, but they soon become second nature.

Andy BoettcherAndy Boettcher

Patrick,

 

Do throrughly and completely digest MattLacey's post on this - he is 100% correct on the proper usage and scope of "this".  Using "this" is a good rule of thumb and best practice even when you technically don't need it to keep variable scoping in check.

 

However - for your specific example, yes, you could remove it and it would still work.

 

-Andy

Patrick DixonPatrick Dixon

Thanks Matt for taking the trouble to explain.

 

However, I'm still completely baffed as to the need for this.

 

In your example you'd reference the car's colour through MyCar.colour or listCars[0].colour from outside the class, but  inside you'd access the colour just through the variable colour.

 

OK, so you could reference it as this.colour but why bother since you're inside the instance of the class and colour will surfice perfectly well?

 

Obviously I'm missing something here!

MattLacey.ax1065MattLacey.ax1065

No worries, I understand your confusion!

 

One use is as in my first example, you can use it to refer to a member variable when there is a local variable or a parameter passed to the method.

 

An alternative use is in cases where you may need to return a reference to the current object, this would be a rather contrived example:

 

// maxSpeed is a member variable of CVehicle
CVehicle GetFastest(CVehicle comparisonVehicle)
{
  if(comparisonVehicle.maxSpeed > maxSpeed)
  {
    return comparisonVehicle;
  }
  else
  {
    return this;
  }
}