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:
instance number &Num;
A method of Example could reference another Example instance's &Num instance variable as follows:
&X = &SomeOtherExample.Num;
%This when used in application
class constructor code refers to the object currently being constructed.
It does not refer to the dynamic this pointer of some object under
construction. Given these two application classes, the PeopleCode
the Base constructor always refers
to the Test method in the Base class.
class Base method Base(); method Test(); method CallTest(); property string sTestResult; end-class; method Base %This.Test(); end-method; method Test &sTestResult = "BASE_METHOD_CALLED"; end-method; method CallTest %This.Test(); end-method; import PACKAGE:Base; class Extend extends PACKAGE:Base; method Extend(); method Test(); end-class; method Extend %Super = create PACKAGE:Base(); end-method; method Test /+ Extends/implements PACKAGE:Base.Test +/ %This.sTestResult = "EXTENSION_METHOD_CALLED"; end-method;
Even though the Extend
class method provides its own Test method which overrides Base's Test
method, in PeopleCode
create Extend() which ultimately runs Base's constructor,
%This.Test() call in Base's constructor still references Base's Test method,
not Extend's Test method, because %This always refers to the object