OO COBOL: Method Definitions

Method definitions reside within the PROCEDURE DIVISION of a class definition. Each one has the usual four divisions, but with special features or restrictions.

IDENTIFICATION DIVISION

Instead of a PROGRAM-ID, code a METHOD-ID:
 METHOD-ID. displayOnSysout.
If you are overriding a method defined in some parent class, add an OVERRIDE clause:
 METHOD-ID. somInit OVERRIDE.

ENVIRONMENT DIVISION

The only section allowed here is an INPUT-OUTPUT section for allocating files. You don't need a REPOSITORY paragraph -- all the classes used should already be declared at the class level.

DATA DIVISION

In the FILE SECTION, any files described must be EXTERNAL.

The LOCAL-STORAGE SECTION is the same as in an ordinary program, except that the GLOBAL attribute has no effect, since you can't have nested programs in a method definition.

This section has no counterpart in VS COBOL II. It declares variables which exist only while the method is executing. They are created when you enter the method, and are destroyed when you exit. They are similar to "auto" variables in C or C++.

The WORKING-STORAGE SECTION works in the usual way, except that GLOBAL has no effect. Items in WORKING-STORAGE occur once per class, not once per instance. Their values persist from one invocation to the next. They are accessible only to the methods which declare them, unless they are EXTERNAL.

The LINKAGE SECTION also works in the usual way, except that GLOBAL has no effect.

PROCEDURE DIVISION

The procedural code looks normal, except that you can't use any of the following constructs: It is surprising that GO TO should be outlawed, but you won't hear any complaints from me.

Termination

Every method definition must end with a terminator:
 END METHOD displayOnSysout.

[home]Cobol Home [OOC]OO Cobol