COBOL class definition structure
Enterprise COBOL provides object-oriented syntax to facilitate interoperation of COBOL and Java™ programs.
You can use object-oriented syntax to:
- Define classes, with methods and data implemented in COBOL
- Create instances of Java or COBOL classes
- Invoke methods on Java or COBOL objects
- Write classes that inherit from Java classes or from other COBOL classes
- Define and invoke overloaded methods
Basic Java-oriented object capabilities are accessed directly through COBOL language features. Additional capabilities are available to the COBOL programmer by calling services through the Java Native Interface (JNI), as described in Accessing JNI services in the Enterprise COBOL Programming Guide.
Java programs can be multithreaded, and Java interoperation requires toleration of asynchronous signals. Therefore, to mix COBOL with these Java programs, you must use the thread enablement provided by the THREAD compiler option, as described in THREAD in the Enterprise COBOL Programming Guide.
Java String data is represented at run time in Unicode. The Unicode support provided in Enterprise COBOL with the national data type enables COBOL programs to exchange String data with Java.
The following entities and concepts are used in object-oriented COBOL for Java interoperability:
- Class
- The entity that defines operations and state for zero, one, or
more object instances and defines operations and state for a common
object (a factory object) that is shared by multiple object instances.
You create object instances using the NEW operand of the COBOL INVOKE statement or using a Java class instance creation expression.
Object instances are automatically freed by the Java runtime system's garbage collection when they are no longer in use. You cannot explicitly free individual objects.
- Instance method
- Procedural code that defines one of the operations supported for
the object instances of a class. Instance methods introduced by a
COBOL class are defined within the object paragraph of the class definition.
COBOL instance methods are equivalent to public nonstatic methods in Java.
You execute instance methods on a particular object instance by using a COBOL INVOKE statement or a Java method invocation expression.
- Instance data
- Data that defines the state of an individual object instance.
Instance data in a COBOL class is defined in the WORKING-STORAGE SECTION
of the DATA DIVISION within the object paragraph of a class definition.
COBOL instance data is equivalent to private nonstatic member data in a Java class.
The state of an object also includes the state of the instance data introduced by inherited classes. Each instance object has its own copy of the instance data defined within its class definition and its own copy of the instance data defined in inherited classes.
You can access COBOL object instance data only from within COBOL instance methods defined in the class definition that defines the data.
You can initialize object instance data with VALUE clauses or you can write an instance method to perform custom initialization.
- Factory method, static method
- Procedural code that defines one of the operations supported
for the common factory object of the class. COBOL factory methods
are defined within the factory paragraph of a class definition. Factory
methods are associated with a class, not with any individual instance
object of the class.
COBOL factory methods are equivalent to public static methods in Java.
You execute COBOL factory methods from COBOL using an INVOKE statement that specifies the class-name as the first operand. You execute them from a Java program using a static method invocation expression.
A factory method cannot operate directly on instance data of its class, even though the data is described in the same class definition; a factory method must invoke instance methods to act on instance data.
COBOL factory methods are typically used to define customized methods that create object instances. For example, you can code a customized factory method that accepts initial values as parameters, creates an instance object using the NEW operand of the INVOKE statement, and then invokes a customized instance method passing those initial values as arguments for use in initializing the instance object.
- Factory data, static data
- Data associated with a class, rather than with an individual
object instance. COBOL factory data is defined in the WORKING-STORAGE
SECTION of the DATA DIVISION within the factory paragraph of a class
definition.
COBOL factory data is equivalent to private static data in Java.
There is a single copy of factory data for a class. Factory data is associated only with the class and is shared by all object instances of the class. It is not associated with any particular instance object. A factory data item might be used, for example, to keep a count of the number of instance objects that have been created.
You can access COBOL factory data only within COBOL factory methods defined in the same class definition.
- Inheritance
- Inheritance is a mechanism whereby a class definition
(the inheriting class) acquires the methods, data descriptions, and
file descriptions written in another class definition (the inherited
class). When two classes in an inheritance relationship are considered
together, the inheriting class is the subclass (or derived
class or child class); the inherited class is the superclass (or
parent class). The inheriting class also indirectly acquires the
methods, data descriptions, and file descriptions that the parent
class inherited from its parent class.
A COBOL class must inherit from exactly one parent class, which can be implemented in COBOL or Java.
Every COBOL class must inherit directly or indirectly from the java.lang.Object class.
- Instance variable
- An individual data item defined in the DATA DIVISION of an object paragraph.
- Java Native Interface (JNI)
- A facility of Java designed for interoperation with non-Java programs.
- Java Native Interface (JNI) environment pointer
- A pointer used to obtain the address of the JNI environment structure used for calling JNI services. The COBOL special register JNIENVPTR is provided for referencing the JNI environment pointer.
- Object reference
- A data item that contains information used to identify and reference an individual object. An object reference can refer to an object that is an instance of a Java or COBOL class.
- Subclass
- A class that inherits from another class; also called a derived class or child class of the inherited class.
- Superclass
- A class that is inherited by another class; also called a parent class of the inheriting class.
With the exception of the COPY and REPLACE statements and the END CLASS marker, the statements, entries, paragraphs, and sections of a COBOL class definition are grouped into the following structure:
- IDENTIFICATION DIVISION
- ENVIRONMENT DIVISION (configuration section only)
- Factory definition
- IDENTIFICATION DIVISION
- DATA DIVISION
- PROCEDURE DIVISION (containing one or more method definitions)
- Object definition
- IDENTIFICATION DIVISION
- DATA DIVISION
- PROCEDURE DIVISION (containing one or more method definitions)
The end of a COBOL class definition is indicated by the END CLASS marker.
The following format is for a COBOL class definition.
- END CLASS
- Specifies the end of a class definition.
- END FACTORY
- Specifies the end of a factory definition.
- END OBJECT
- Specifies the end of an object definition.