COBOL method definition structure
A COBOL method definition describes a method. You can specify method definitions only within the factory paragraph and the object paragraph of a class definition.
With the exception of COPY and REPLACE statements and the END METHOD marker, the statements, entries, paragraphs, and sections of a COBOL method definition are grouped into the following four divisions:
- IDENTIFICATION DIVISION
- ENVIRONMENT DIVISION (input-output section only)
- DATA DIVISION
- PROCEDURE DIVISION
The end of a COBOL method definition is indicated by the END METHOD marker.
The following format is for a COBOL method definition.
- METHOD-ID
- Identifies a method definition. See METHOD-ID paragraph for details.
- method-procedure-division-header
- Indicates the start of the PROCEDURE DIVISION and identifies method parameters and the returning item, if any. See The PROCEDURE DIVISION header for details.
- END METHOD
- Specifies the end of a method definition.
Methods defined in an object definition are instance methods. An instance method in a given class can access:
- Data defined in the DATA DIVISION of the object paragraph of that class (instance data)
- Data defined in the DATA DIVISION of that instance method (method data)
An instance method cannot directly access instance data defined in a parent class, factory data defined in its own class, or method data defined in another method of its class. It must invoke a method to access such data.
Methods defined in a factory definition are factory methods. A factory method in a given class can access:
- Data defined in the DATA DIVISION of the factory paragraph of that class (factory data)
- Data defined in the DATA DIVISION of that factory method (method data)
A factory method cannot directly access factory data defined in a parent class, instance data defined in its own class, or method data defined in another method of its class. It must invoke a method to access such data.
Methods can be invoked from COBOL programs and methods, and they can be invoked from Java™ programs. A method can execute an INVOKE statement that directly or indirectly invokes itself. Therefore, COBOL methods are implicitly recursive (unlike COBOL programs, which support recursion only if the RECURSIVE attribute is specified in the program-ID paragraph.)