Chapter 6. A Closer Look at Objects

Table of Contents
Using Objects in Rexx
Common Methods
Special Method Variables
Public, Local, and Built-In Environment Objects
Determining the Scope of Methods and Variables
More about Methods

This chapter covers the mechanics of using objects in more detail. First, a quick refresher.

A Rexx object consists of:

Sending a message to an object causes it to perform a related action. The method with the matching name performs the action. The message is the interface to the object, and with information hiding, only methods that belong to an object can access its variables.

Objects are grouped hierarchically into classes. The class at the top of the hierarchy is the Object class. Everything below it in the hierarchy belongs to the Object class and is therefore an object. As a result, all classes are objects.

In a class hierarchy, classes, superclasses, and subclasses are relative to one another. Unless designated otherwise, any class directly above a class in the hierarchy is a superclass. And any class below is a subclass.

From a class you can create instances of the class. Instances are merely similar objects that fit the template of the class; they are "of" the the class, but are not classes themselves.

Both the classes and their instances contain variables and methods. The methods a class provides for use by its instances are called instance methods. The instance methods define which messages an object can respond to.

The methods available to the class itself are called class methods. Many of the methods are actually the instance methods of the Class class, but a class many have its own unique class methods. They define messages that only the class—and not its instances—can respond to.

Figure 6-1. Instance Methods and Class Methods

Using Objects in Rexx

The following examples with Myarray illustrate how to use new objects in Rexx programs.


creates a new instance of the Array class, and assigns to the variable MYARRAY. The period precedes a class name in an expression, to distinguish the class environment symbol from other variables. The MYARRAY array object has five elements.

After the array is created, you can assign values to it. One way is with the PUT method. PUT has two arguments, which must be enclosed in parentheses. The first argument is the value added to the array, the second is the number of the element in which to place the value. Here, the string object Hello is associated with the third element of Myarray:


One way to retrieve values from an array object is by sending it an AT message. In the next example, the SAY instruction displays the third element of Myarray:

say myarray~at(3)



The SAY instruction expects a string object as input, which is what AT returns. If you try to display a non-string object in the SAY instruction, SAY sends a STRING message to the object. The STRING method returns a human-readable string representation for the object. In this example, the STRING method for an Array object returns the string an Array:

say myarray  /* SAY sends STRING message to Myarray */


an Array

Whenever a method returns a string, you can use it within expressions that require a string. Here, the element of the array that AT returns is a string, so you can put an expression containing the AT method inside a string function like COPIES():

say copies(myarray~at(3),4)


This example produces the same result using only methods:

say myarray~at(3)~copies(4)

Notice that the expression is evaluated from left to right. You can also use parentheses to enforce an order of evaluation.

Almost all messages are sent using the twiddle, but there are exceptions. The exceptions are to improve the reliability of the language. The following example uses the []= (left-bracket right-bracket equal-sign) and [] methods to set and retrieve array elements:

myarray[4]="the fourth element"
say myarray[4]

Although the previous instructions look like an ordinary array assignment and array reference, they are actually messages to the Array object referenced by MYARRAY. You can prove this by executing these equivalent instructions, which use the twiddle to send the messages:

myarray~"[]="("a new test",4)
say myarray~"[]"(4)

Similarly, expression operators (such as +, -, /, and *) are actually methods, but you do not have to use the twiddle to send them:

say 2+3      /* Displays 5 */
say 2~"+"(3) /* Displays 5 */

In the second SAY instruction, "+" must be a literal string because the message name contains characters not allowed in a Rexx symbol.