Instantiating COBOL classes

To instantiate a COBOL class, you can specify either a typed or universal object reference in the RETURNING phrase of the INVOKE . . . NEW statement. However, you cannot code the USING phrase: the instance data is initialized as specified in the VALUE clauses in the class definition.

Thus the INVOKE . . . NEW statement is useful for instantiating COBOL classes that have only simple instance data. For example, the following statement creates an instance of the Account class, initializes the instance data as specified in VALUE clauses in the WORKING-STORAGE SECTION of the OBJECT paragraph of the Account class definition, and provides reference outAccount to the new instance:


Invoke Account New returning outAccount

To make it possible to initialize COBOL instance data that cannot be initialized using VALUE clauses alone, when designing a COBOL class you must define a parameterized creation method in the FACTORY paragraph and a parameterized initialization method in the OBJECT paragraph:

  1. In the parameterized factory creation method, do these steps:
    1. Code INVOKE class-name NEW RETURNING objectRef to create an instance of class-name and to give initial values to the instance data items that have VALUE clauses.
    2. Invoke the parameterized initialization method on the instance (objectRef), passing BY VALUE the arguments that were supplied to the factory method.
  2. In the initialization method, code logic to complete the instance data initialization using the values supplied through the formal parameters.

To create an instance of the COBOL class and properly initialize it, the client invokes the parameterized factory method, passing BY VALUE the required arguments. The object reference returned to the client is a local reference. If the client code is within a method, and the use of the returned object reference is not limited to the duration of that method, the client code must convert the returned object reference to a global reference by calling the JNI service NewGlobalRef.

Example: defining a factory (with methods)

related references  
VALUE clause (Enterprise COBOL for z/OS® Language Reference)  
INVOKE statement (Enterprise COBOL for z/OS Language Reference)