In OO COBOL, a Class is a COBOL program written with a class identifier. We will look at examples of classes in the next chapter. At runtime, a program that desires to call or connect to a COBOL Class will create a new instance of this class first. This instance of the COBOL Class is known as an Object.
In OO COBOL programming, an OO COBOL Class can have an object instantiated by a traditional procedural COBOL program using just a bit of OO COBOL syntax.
When an OO COBOL class is called to instantiate an object (remember that the object is the runtime executable instance of the OO COBOL program defined in the OO COBOL class), it returns a pointer (sometimes called a handle) to the newly created object. This pointer is stored in a COBOL data item defined as USAGE OBJECT REFERENCE.
Figure 1.1 shows a traditional procedural COBOL program that instantiates a COBOL class named FILEIO4 and executes various Methods in it.
Identification Division. Program-ID. TESTFILEIO4. Environment Division. Configuration Section. Repository. Class FILEIO4. Data Division. Working-Storage Section. 01 FILEIO-PTR Usage Object Reference. 01 File-Operation Pic X(5). 01 Return-Status Pic 99 Value 0. 01 Return-Record Pic X(132). Procedure Division. Invoke FILEIO4 "NEW" Returning FILEIO-PTR. Invoke FILEIO-PTR "OPEN-FILE" Using File-Operation Return-Status Return-Record. If Return-Status = 0 Perform Until Return-Status Not = 0 Invoke FILEIO-PTR "READ-FILE" Using File-Operation Return-Status Return-Record If Return-Status = 0 Display Return-Record End-If End-Perform Invoke FILEIO-PTR "CLOSE-FILE" Using File-Operation Return-Status Return-Record End-If. Set FILEIO-PTR To Null. Exit Program.
Figure 1.1 A sample COBOL program instantiating an OO COBOL class
In examining the program in Figure 1.1, you will note the insertion of a Repository under the ENVIRONMENT DIVISION’S CONFIGURATION SECTION. The Repository specifies the name of any OO COBOL classes that the program may reference at runtime.
In the PROCEDURE DIVISION, you will notice the following statement:
Invoke FILEIO4 "NEW" Returning FILEIO-PTR.
Invoke is an OO COBOL verb that is similar to a CALL statement. Invoke will always call an OO COBOL Class (or other language COM or OLE object) and execute a specific Method. The Method being Invoked (called) in the previous example is the “NEW” method, which creates a new instance (executable) of the FILEIO4 class (program). This statement returns a pointer to the new instance of the class, which is stored in the WORKING-STORAGE data item:
01 FILEIO-PTR Usage Object Reference.
If you examine the procedural code further, you will note additional invoke statements such as:
Invoke FILEIO-PTR "READ-FILE" Using File-Operation Return-Status Return-Record.
Invoking FILEIO-PTR actually calls the instance of the FILEIO4 class that was created with the invocation of its “NEW” method. The previous example is now calling the “READ-FILE” method of the FILEIO4 object that was created and is passing three parameters to it via the USING statement, which is identical to the way parameters are passed in a traditional CALL statement.
Just before the program is exited, you will note the following statement:
Set FILEIO-PTR To Null.
By setting the OBJECT REFERENCE named FILEIO-PTR to Null, we release this instance of the class (release the object), thus deleting it from memory. It is important to do this or you can leave orphan objects laying around in memory. In COBOL terms, this is similar to executing a CANCEL statement.
Let’s have a look at the actual OO COBOL class (program) being instantiated in this example (FILEIO4).
Figure 1.2 contains a listing of the FILEIO4 OO COBOL program.
Identification Division. Class-ID. FILEIO4 Inherits FJBASE. Environment Division. Configuration Section. Repository. Class FJBASE. Object. Environment Division. Input-Output Section. File-Control. Select Infile Assign To "test.txt" Organization is Line Sequential File Status is Infile-Status. Data Division. File Section. FD Infile. 01 Infile-Record Pic X(132). Working-Storage Section. 01 Infile-Status Pic 99. 01 File-Opened-Flag Pic 9 Value 0. Procedure Division. Method-ID. OPEN-FILE. Data Division. Linkage Section. 01 File-Operation Pic X(5). 01 Return-Status Pic 99. 01 Return-Record Pic X(132). Procedure Division Using File-Operation Return-Status Return-Record. If File-Opened-Flag Not = 0 *> File is already open, so return error Move 99 To Return-Status Else Open Input Infile Move Infile-Status To Return-Status If Infile-Status = 0 Move 1 To File-Opened-Flag End-If End-If. End Method OPEN-FILE. Method-ID. CLOSE-FILE. Data Division. Linkage Section. 01 File-Operation Pic X(5). 01 Return-Status Pic 99. 01 Return-Record Pic X(132). Procedure Division Using File-Operation. Return-Status Return-Record. If File-Opened-Flag = 0 *> File is not open, so return error Move 88 To Return-Status Else Close Infile Move Infile-Status To Return-Status Move 0 To File-Opened-Flag End-If. End Method CLOSE-FILE. Method-ID. READ-FILE. Data Division. Linkage Section. 01 File-Operation Pic X(5). 01 Return-Status Pic 99. 01 Return-Record Pic X(132). Procedure Division using File-Operation Return-Status Return-Record. If File-Opened-Flag = 0 *> File is not open, so return error Move 88 To Return-Status Else Read Infile Move Infile-Status to Return-Status Move Infile-Record to Return-Record End-If. End Method READ-FILE. End Object. End Class FILEIO4.
Figure 1.2 The FILEIO4 OO COBOL Class (Program)