Initializing Instances Using INIT

Object classes can create instances. When these instances require initialization, you'll want to define an INIT method to set a particular starting value or initiate some startup processing. Rexx looks for an INIT method whenever a new object is created and runs it.

The purpose of initialization is to ensure that the instance variables can be initialized, if needed, before being used in an operation. After the NEW method has created the new instance, but before returning it, the new instance gets the INIT message sent to it. Any (initialization) arguments specified with the NEW message are passed on to the INIT method in the same order, which can use them to set the initial states of object variables. As the INIT method gets invoked automatically at object creation time it is also known as the Rexx constructor method.

If a class overrides the INIT method it inherits from a superclass, the new INIT method must forward the INIT message up the hierarchy, to properly initialize the instance, using the SUPERCLASS overrides, so that each inherited INIT method has the opportunity to run (using e.g. the statement self~init:super). An example in the next section demonstrates the use of the Rexx constructor method INIT.