In PeopleSoft application classes, instance variables are analogous to private variables in most object-oriented languages. This means that instance variables are inaccessible from PeopleCode outside the class where it is declared. I used to assume that a specific object (instance of a class) would only have access to its own instance variables. This appears not to be the case, as the following paragraphs in PeopleBooks (PeopleCode API Reference > Application Class > Self-Reference) states:

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;


To experiment with this, I created this simple application class:


class Example
   method CreateNewInstance();
   method IncrementNewInstanceNum() Returns number;
   method IncrementThisInstanceNum() Returns number;
   method IncrementPassedNum(&passed As Example);
   property number ThisInstanceNum get;
private
   instance Example &newInstance;
   instance number &Num;
end-class;
 
method CreateNewInstance
   &newInstance = create Example();
end-method;
 
method IncrementNewInstanceNum
   /+ Returns Number +/
   &newInstance.Num = &newInstance.Num + 1;
   Return &newInstance.Num;
end-method;
 
method IncrementThisInstanceNum
   /+ Returns Number +/
   &Num = &Num + 1;
   Return &Num;
end-method;
 
method IncrementPassedNum
   /+ &passed as CJ:Example +/
   &passed.Num = &passed.Num + 1;
end-method;
 
get ThisInstanceNum
   /+ Returns Number +/
   Return &Num;
end-get;

Then referenced the class in the following manner:


Local Example &test1 = create Example();
&test1.CreateNewInstance();
 
MessageBox(0, "", 0, 0, "IncrementNewInstanceNum() returns %1", &test1.IncrementNewInstanceNum());
MessageBox(0, "", 0, 0, "ThisInstanceNum returns %1", &test1.ThisInstanceNum);
MessageBox(0, "", 0, 0, "IncrementThisInstanceNum() returns %1", &test1.IncrementThisInstanceNum());
MessageBox(0, "", 0, 0, "IncrementNewInstanceNum() returns %1", &test1.IncrementNewInstanceNum());
 
Local Example &test2 = create Example();
MessageBox(0, "", 0, 0, "&test2.ThisInstanceNum returns %1", &test2.ThisInstanceNum);
&test1.IncrementPassedNum(&test2);
MessageBox(0, "", 0, 0, "&test2.ThisInstanceNum returns %1", &test2.ThisInstanceNum);

This yields the following result:

IncrementNewInstanceNum() returns 1
ThisInstanceNum returns 0
IncrementThisInstanceNum() returns 1
IncrementNewInstanceNum() returns 2
&test2.ThisInstanceNum returns 0
&test2.ThisInstanceNum returns 1

This shows that indeed, an object has unrestricted access to the private instance variables of another instance of the same class. However, one thing that I am not quite sure of is why is this allowed? When is this needed? Do any other programming languages out there has the same characteristic as this?

Update: Further down on the same PeopleBook, on the section Declaring Private Instance Variables, an example scenario was presented:

A private instance variable is private to the class, not just to the object instance. For example, consider a linked-list class where one instance needs to update the pointer in another instance.