Defining a class

A COBOL class definition consists of an IDENTIFICATION DIVISION and ENVIRONMENT DIVISION, followed by an optional factory definition and optional object definition, followed by an END CLASS marker.

About this task

Table 1. Structure of class definitions
Section Purpose Syntax
IDENTIFICATION DIVISION (required) Name the class. Provide inheritance information for it.
CLASS-ID paragraph for defining a class (required)

AUTHOR paragraph (optional)
INSTALLATION paragraph (optional)
DATE-WRITTEN paragraph (optional)
DATE-COMPILED paragraph (optional)

ENVIRONMENT DIVISION (required) Describe the computing environment. Relate class-names used within the class definition to the corresponding external class-names known outside the compilation unit.

CONFIGURATION SECTION (required)

REPOSITORY paragraph for defining a class (required)

SOURCE-COMPUTER paragraph (optional)
OBJECT-COMPUTER paragraph (optional)
SPECIAL-NAMES paragraph (optional)

Factory definition (optional) Define data to be shared by all instances of the class, and methods supported independently of any object instance.
 IDENTIFICATION DIVISION.
 FACTORY.
  DATA DIVISION.
  WORKING-STORAGE SECTION.
*   (Factory data here)
  PROCEDURE DIVISION.
*   (Factory methods here)
 END FACTORY.
Object definition (optional) Define instance data and instance methods.
 IDENTIFICATION DIVISION.
 OBJECT.
  DATA DIVISION.
  WORKING-STORAGE SECTION.
*   (Instance data here)
  PROCEDURE DIVISION.
*   (Instance methods here)
 END OBJECT.

If you specify the SOURCE-COMPUTER, OBJECT-COMPUTER, or SPECIAL-NAMES paragraphs in a class CONFIGURATION SECTION, they apply to the entire class definition including all methods that the class introduces.

A class CONFIGURATION SECTION can consist of the same entries as a program CONFIGURATION SECTION, except that a class CONFIGURATION SECTION cannot contain an INPUT-OUTPUT SECTION. You define an INPUT-OUTPUT SECTION only in the individual methods that require it rather than defining it at the class level.

As shown above, you define instance data and methods in the DATA DIVISION and PROCEDURE DIVISION, respectively, within the OBJECT paragraph of the class definition. In classes that require data and methods that are to be associated with the class itself rather than with individual object instances, define a separate DATA DIVISION and PROCEDURE DIVISION within the FACTORY paragraph of the class definition.

Each COBOL class definition must be in a separate source file.

Example: defining a class

related references  
COBOL class definition structure (COBOL for AIX Language Reference)