Defining a factory section

Use the FACTORY paragraph in a class definition to define data and methods that are to be associated with the class itself rather than with individual object instances.

COBOL factory data is equivalent to Java™ private static data. A single copy of the data is instantiated for the class and is shared by all object instances of the class. You most commonly use factory data when you want to gather data from all the instances of a class. For example, you could define a factory data item to keep a running total of the number of instances of the class that are created.

COBOL factory methods are equivalent to Java public static methods. The methods are supported by the class independently of any object instance. You most commonly use factory methods to customize object creation when you cannot use VALUE clauses alone to initialize instance data.

By contrast, you use the OBJECT paragraph in a class definition to define data that is created for each object instance of the class, and methods that are supported for each object instance of the class.

A factory definition consists of three divisions, followed by an END FACTORY statement:

Table 1. Structure of factory definitions
Division Purpose Syntax
IDENTIFICATION (required) Identify the start of the factory definition.

DATA (optional) Describe data that is allocated once for the class (as opposed to data allocated for each instance of a class). WORKING-STORAGE SECTION for defining factory data (optional)
PROCEDURE (optional) Define factory methods. Factory method definitions: Defining a factory method

Example: defining a factory (with methods)