MainframeMaster

COBOL Tutorial

COBOL OBJECT - Quick Reference

Progress0 of 0 lessons

Overview

An OBJECT in object-oriented COBOL is an instance of a class that contains data (attributes) and can execute methods. Objects are created at runtime and represent specific instances of the class template.

Purpose and Usage

  • Runtime instances of classes
  • Data encapsulation and behavior
  • Method execution on object instances
  • Object-oriented programming support

Syntax

Object creation and usage in COBOL involves several components:

Object Reference Declaration

cobol
1
2
3
4
* Declaring object references 01 my-object OBJECT REFERENCE. 01 employee-obj OBJECT REFERENCE Employee. 01 calculator-obj OBJECT REFERENCE Calculator.

OBJECT REFERENCE declares variables that can hold object instances.

Object Creation

cobol
1
2
3
4
5
6
7
8
9
* Creating objects using factory methods INVOKE Employee "NEW" RETURNING employee-obj. * Alternative creation syntax SET employee-obj TO NEW Employee. * Creating with parameters INVOKE Employee "NEW" USING "John Doe", 1001 RETURNING employee-obj.

Method Invocation

cobol
1
2
3
4
* Invoking methods on objects INVOKE employee-obj "GetName" RETURNING employee-name. INVOKE employee-obj "SetSalary" USING new-salary. INVOKE calculator-obj "Add" USING a, b RETURNING result.

Practical Examples

Here are some practical uses of objects in COBOL:

Employee Object Usage

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
* Working with employee objects 01 employee1 OBJECT REFERENCE Employee. 01 employee2 OBJECT REFERENCE Employee. 01 emp-name PIC X(30). 01 emp-salary PIC 9(7)V99. PROCEDURE DIVISION. MAIN-PROCESS. * Create employee objects INVOKE Employee "NEW" USING "John Doe", 50000 RETURNING employee1. INVOKE Employee "NEW" USING "Jane Smith", 60000 RETURNING employee2. * Access object methods INVOKE employee1 "GetName" RETURNING emp-name. INVOKE employee1 "GetSalary" RETURNING emp-salary. DISPLAY "Employee: " emp-name. DISPLAY "Salary: " emp-salary. * Modify object state INVOKE employee1 "SetSalary" USING 55000. STOP RUN.

Creating and manipulating employee objects.

Calculator Object Usage

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
* Working with calculator objects 01 calc OBJECT REFERENCE Calculator. 01 result PIC 9(6)V99. 01 a PIC 9(3) VALUE 10. 01 b PIC 9(3) VALUE 20. PROCEDURE DIVISION. CALCULATE. * Create calculator object INVOKE Calculator "NEW" RETURNING calc. * Perform calculations INVOKE calc "Add" USING a, b RETURNING result. DISPLAY "Sum: " result. INVOKE calc "Multiply" USING a, b RETURNING result. DISPLAY "Product: " result. INVOKE calc "Divide" USING b, a RETURNING result. DISPLAY "Quotient: " result. STOP RUN.

Using calculator objects for mathematical operations.

Best Practices

  • Use meaningful names for object references.
  • Always check if object references are valid before use.
  • Properly manage object lifecycle (creation and destruction).
  • Use encapsulation to protect object data.
  • Document object interfaces and expected behavior.

Common Pitfalls

  • Using uninitialized object references.
  • Not checking for null object references before method calls.
  • Memory leaks from not properly disposing of objects.
  • Incorrect method signatures when invoking object methods.

Test Your Knowledge

1. What is an OBJECT in object-oriented COBOL?

  • A data structure
  • An instance of a class
  • A file
  • A program

2. How do you create an object in COBOL?

  • Using the CREATE statement
  • Using the NEW statement
  • Using the FACTORY method
  • Using the INSTANTIATE statement

3. What does the OBJECT-REFERENCE data type represent?

  • A pointer to data
  • A reference to an object instance
  • A file handle
  • A memory address

4. How do you invoke methods on an object?

  • Using PERFORM
  • Using INVOKE
  • Using CALL
  • Using EXECUTE

5. What is the relationship between CLASS and OBJECT?

  • They are the same
  • CLASS is the template, OBJECT is the instance
  • OBJECT is the template, CLASS is the instance
  • They are unrelated

Frequently Asked Questions