0

I was writing an Apex class, and wasn't referring to this whenever I referenced an instance variable. My tests were working. When I noticed this, I tried prepending this. to each of the instance variable references, and the tests still worked.

The documentation only uses this during assignment.

So is it required for assignment but optional for referencing?

3

this is used to remove any ambiguity between a function-level property and a class level property. A few examples:

public class X {
  public String Y;
  public void ex1(String Y) {
    this.Y = Y; // assign parameter to class member
  }
  public void ex2() {
    String y = this.Y; // assign class member to local variable
  }
  public void ex3() {
    System.debug(y); // "this" is implied, as no local shadow
  }
}

Similarly, once you get in to inheritance, you may also want to reference a parent variable by the same name:

virtual class C1 {
    protected String y = 'hello';
}
class C2 extends C1 {
    protected String y = 'world';
    void debugSuper() {
        System.debug(super.y);
    }
    void debugSelf() {
        System.debug(this.y);
        System.debug(y); // "this" variable would be found first, not super
    }
}
public static void exec() {
    c2 x = new c2();
    x.debugSuper();
    x.debugSelf();
}

The same thing also applies to methods, which is where it gets really useful; you may have a method that's overridden and you want to call the base method for some reason. You can use super to do so.

As stated by @PhilW, it's best if you don't use this unless you have to. It's usually easier and more convenient to use unambiguous names in your code to minimize the use of this and/or super.

The absolute only time you need to use this is if you have a local property that is shadowing a class-level property.

Similar things can also happen with static variables. If you need to reference a variable that is static, you can always refer to it by full name:

class C1 {
  public static String value;
  public void exec() {
    String value = C1.value;
  }
}
3

You only need to qualify a variable with this when there is a parameter variable, in a method of the class, with the same name as the instance variable.

This allows the language to pick up the parameter without any context while allowing you to still access the instance variable/property via the use of the this context.

It is always permitted to qualify access to an instance variable/property with this in methods belonging to the class, or a subclass thereof. However, you should only use it when you absolutely have to in order to avoid unnecessary typing (recall there are limits on the amount of code you can have in apex and this text contributes to that limit).

1
  • I find it useful when coming back to classes I wrote long ago to be able to see within a method which variables are instance variables (hence this.xxx) vs locally-scope method variables. Helps me remember what I was doing in the past – cropredy Apr 18 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.