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.
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)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. | |
Object definition (optional) | Define instance data and instance methods. | |
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.
WORKING-STORAGE SECTION for defining class instance data
Defining a class instance method
Defining a subclass
Defining a factory section
Describing the computing environment
Compiling, linking, and running OO applications