WORKING-STORAGE SECTION for defining class instance data

Use the WORKING-STORAGE SECTION in the DATA DIVISION of the OBJECT paragraph to describe the instance data that a COBOL class needs, that is, the data to be allocated for each instance of the class.

About this task

The OBJECT keyword, which you must immediately precede with an IDENTIFICATION DIVISION declaration, indicates the beginning of the definitions of the instance data and instance methods for the class. For example, the definition of the instance data for the Account class might look like this:


IDENTIFICATION DIVISION.
Object.
  DATA DIVISION.
  WORKING-STORAGE SECTION.
  01 AccountNumber  pic 9(6).
  01 AccountBalance pic S9(9) value zero.
  . . .
End Object.

The instance data is allocated when an object instance is created, and exists until garbage collection of the instance by the Java™ run time.

You can initialize simple instance data by using VALUE clauses as shown above. You can initialize more complex instance data by coding customized methods to create and initialize instances of classes.

COBOL instance data is equivalent to Java private nonstatic member data. No other class or subclass (nor factory method in the same class, if any) can reference COBOL instance data directly. Instance data is global to all instance methods that the OBJECT paragraph defines. If you want to make instance data accessible from outside the OBJECT paragraph, define attribute (get or set) instance methods for doing so.

The syntax of the WORKING-STORAGE SECTION for instance data definition is generally the same as in a program, with these exceptions:

  • You cannot use the EXTERNAL attribute.
  • You can use the GLOBAL attribute, but it has no effect.