Chapter 6. A Closer Look at Objects

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

This chapter covers the mechanics of using objects and methods 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 whose name matches the message 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 belong to the class, but are not classes themselves. Instances are the most basic, most elemental of objects.

Both the classes and their instances contain variables and methods. The methods a class provides for use by its various instances are called instance methods. In effect, these define which messages an instance can respond to. Instance methods are the most common methods in Rexx, and are therefore called methods.

The methods available to the class itself are called class methods. (They are actually the instance methods of the Class class.) They define messages that only the class--and not its instances--can respond to. Class methods generally exist to create instances and are much less common.

Figure 6-1. Instance Methods and Class Methods

Using Objects in Rexx Clauses

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

myarray=.array~new(5)

creates a new instance of the Array class, Myarray. The period precedes a class name in an expression, to distinguish the class from other variables. The Myarray array object has five elements.

After Myarray 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 string to be placed in the element. The second is the number of the element in which to place the string. Here, the string object Hello is associated with the third element of Myarray:

myarray~put("Hello",3)

Rexx dynamically adjusts the size of the array to accommodate the new element.

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)

Results:

Hello

The SAY instruction expects the string object as input, which is what AT returns. If you put a nonstring object in the SAY instruction, SAY sends a STRING message to the object. The STRING method for Rexx's built-in objects returns a human-readable string representation for the object. In this example, the STRING method for Myarray returns the string an array:

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

Results:

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)

Results:
HelloHelloHelloHello

This example gets 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 Myarray object. 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, quotes are needed around the + message because it is a character not allowed in a Rexx symbol.