Self-Reference
A method can refer to the current object using the %This system variable.
Due to subtyping, %This is an object of either the method's class or a subclass of the method's class.
In PeopleCode, the methods and properties of the class cannot be used without an object qualification.
In the following code example, the line in bold indicates an error. You can't call the class this way: you must use %This.
class FactorialClass
method factorial(&I as number) returns number;
end-class;
method factorial
if &I <= 1 then
return 1;
end-if;
return &I * factorial(&I - 1); /* error - factorial undefined */
return &I * %This.factorial(&I - 1); /* okay */
end-method;One of the implications of this is that when you're writing a method, there isn't any way to guarantee you'll access your own class' version of a public method (rather then a subclass') as it might be overridden by a subclass. The only way to make sure that you can access your own method is to make it private.
If you have some action that you also want to make public, and you want guaranteed access to it, you can make the action a private method, then define a public method for it that calls the private one.
In the following example, factorial is the public method that can be used by other classes. myFactorial is the private action.
class FactorialClass
method factorial(&I as number) returns number;
private
method myFactorial(&I as number) returns number;
end-class;
method factorial
return myFactorial(&I);
end-method;
method myFactorial
if &I <= 1 then
return 1;
end-if;
return &I * %This.myFactorial(&I - 1);
end-method;If you declare an instance variable as private you can still access it as a private property in another instance of the same class. For example, given the following declaration:
class Example private instance number &Num;end-class;A method of Example could reference another Example instance's &Num instance variable as follows:
&X = &SomeOtherExample.Num;Related Topics