OBJECT-REFERENCE is a data type in object-oriented COBOL that holds a reference to an object (an instance of a class). Think of it as a "remote control" that lets you work with objects - you don't directly manipulate the object, but you use the reference to tell the object what to do.
Imagine you're building a house:
Just like you need an address to find a house, you need an OBJECT-REFERENCE to work with an object.
Using OBJECT-REFERENCE involves declaring variables, creating objects, and invoking methods. Let's look at the basic steps.
123456789101112131415161718192021222324252627282930313233343536* Basic OBJECT-REFERENCE example IDENTIFICATION DIVISION. PROGRAM-ID. OBJECT-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. * Declare an object reference variable 01 CUSTOMER-OBJECT OBJECT-REFERENCE. 01 ACCOUNT-OBJECT OBJECT-REFERENCE. 01 RESULT-VALUE PIC X(50). PROCEDURE DIVISION. * Create a new customer object INVOKE Customer "NEW" RETURNING CUSTOMER-OBJECT * Set customer properties INVOKE CUSTOMER-OBJECT "setName" USING BY CONTENT "John Doe" INVOKE CUSTOMER-OBJECT "setAccountNumber" USING BY CONTENT "12345" * Get customer information INVOKE CUSTOMER-OBJECT "getName" RETURNING RESULT-VALUE DISPLAY "Customer Name: " RESULT-VALUE * Create an account object INVOKE Account "NEW" RETURNING ACCOUNT-OBJECT * Link customer to account INVOKE CUSTOMER-OBJECT "setAccount" USING BY REFERENCE ACCOUNT-OBJECT STOP RUN.
This example shows the basic pattern: declare, create, use, and invoke methods.
Declaration | Purpose | Example |
---|---|---|
01 var-name OBJECT-REFERENCE | Single object reference | 01 CUSTOMER-OBJ OBJECT-REFERENCE |
01 var-name OBJECT-REFERENCE OCCURS n TIMES | Array of object references | 01 CUSTOMER-LIST OBJECT-REFERENCE OCCURS 10 TIMES |
01 var-name OBJECT-REFERENCE CLASS class-name | Typed object reference | 01 CUSTOMER-OBJ OBJECT-REFERENCE CLASS Customer |
01 var-name OBJECT-REFERENCE INITIAL NULLS | Initialized to null | 01 CUSTOMER-OBJ OBJECT-REFERENCE INITIAL NULLS |
Let's look at some real-world examples of how OBJECT-REFERENCE is used in COBOL applications.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768* Customer management with object references IDENTIFICATION DIVISION. PROGRAM-ID. CUSTOMER-MANAGER. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-OBJECT OBJECT-REFERENCE. 01 ACCOUNT-OBJECT OBJECT-REFERENCE. 01 CUSTOMER-NAME PIC X(50). 01 ACCOUNT-NUMBER PIC 9(10). 01 BALANCE PIC 9(10)V99. 01 ACTION-CODE PIC X(1). PROCEDURE DIVISION. * Create customer object INVOKE Customer "NEW" RETURNING CUSTOMER-OBJECT * Set up customer information MOVE "John Smith" TO CUSTOMER-NAME MOVE "1234567890" TO ACCOUNT-NUMBER MOVE 1000.00 TO BALANCE INVOKE CUSTOMER-OBJECT "setName" USING BY CONTENT CUSTOMER-NAME INVOKE CUSTOMER-OBJECT "setAccountNumber" USING BY CONTENT ACCOUNT-NUMBER * Create account object INVOKE Account "NEW" RETURNING ACCOUNT-OBJECT INVOKE ACCOUNT-OBJECT "setBalance" USING BY CONTENT BALANCE * Link customer to account INVOKE CUSTOMER-OBJECT "setAccount" USING BY REFERENCE ACCOUNT-OBJECT * Perform customer operations PERFORM UNTIL ACTION-CODE = "Q" DISPLAY "Customer Operations:" DISPLAY "1 - View Customer Info" DISPLAY "2 - Deposit Money" DISPLAY "3 - Withdraw Money" DISPLAY "Q - Quit" ACCEPT ACTION-CODE EVALUATE ACTION-CODE WHEN "1" PERFORM DISPLAY-CUSTOMER-INFO WHEN "2" PERFORM DEPOSIT-MONEY WHEN "3" PERFORM WITHDRAW-MONEY END-EVALUATE END-PERFORM STOP RUN. DISPLAY-CUSTOMER-INFO. INVOKE CUSTOMER-OBJECT "displayInfo". DEPOSIT-MONEY. DISPLAY "Enter amount to deposit: " ACCEPT BALANCE INVOKE ACCOUNT-OBJECT "deposit" USING BY CONTENT BALANCE. WITHDRAW-MONEY. DISPLAY "Enter amount to withdraw: " ACCEPT BALANCE INVOKE ACCOUNT-OBJECT "withdraw" USING BY CONTENT BALANCE.
This example shows a complete customer management system using object references.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253* Polymorphic shape calculator using object references IDENTIFICATION DIVISION. PROGRAM-ID. SHAPE-CALCULATOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SHAPE-OBJECT OBJECT-REFERENCE. 01 SHAPE-TYPE PIC X(10). 01 LENGTH PIC 9(3)V99. 01 WIDTH PIC 9(3)V99. 01 RADIUS PIC 9(3)V99. 01 AREA PIC 9(6)V99. PROCEDURE DIVISION. DISPLAY "Shape Calculator" DISPLAY "Enter shape type (RECTANGLE/CIRCLE): " ACCEPT SHAPE-TYPE * Create appropriate shape object based on type EVALUATE SHAPE-TYPE WHEN "RECTANGLE" DISPLAY "Enter length: " ACCEPT LENGTH DISPLAY "Enter width: " ACCEPT WIDTH INVOKE Rectangle "NEW" USING BY CONTENT LENGTH WIDTH RETURNING SHAPE-OBJECT WHEN "CIRCLE" DISPLAY "Enter radius: " ACCEPT RADIUS INVOKE Circle "NEW" USING BY CONTENT RADIUS RETURNING SHAPE-OBJECT WHEN OTHER DISPLAY "Unknown shape type" STOP RUN END-EVALUATE * Calculate area using polymorphic method call INVOKE SHAPE-OBJECT "calculateArea" RETURNING AREA DISPLAY "Area: " AREA * Display shape information INVOKE SHAPE-OBJECT "displayInfo" STOP RUN.
This example demonstrates polymorphism - the same method call works for different object types.
OBJECT-REFERENCE supports advanced features for complex object-oriented programming scenarios.
12345678910111213141516171819202122232425262728293031323334353637383940* Working with collections of objects IDENTIFICATION DIVISION. PROGRAM-ID. OBJECT-COLLECTION. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-LIST OBJECT-REFERENCE OCCURS 10 TIMES. 01 CUSTOMER-COUNT PIC 9(2) VALUE 0. 01 I PIC 9(2). 01 CUSTOMER-NAME PIC X(50). 01 CUSTOMER-OBJECT OBJECT-REFERENCE. PROCEDURE DIVISION. * Add customers to the collection PERFORM ADD-CUSTOMER UNTIL CUSTOMER-COUNT >= 10 * Display all customers PERFORM VARYING I FROM 1 BY 1 UNTIL I > CUSTOMER-COUNT INVOKE CUSTOMER-LIST(I) "getName" RETURNING CUSTOMER-NAME DISPLAY "Customer " I ": " CUSTOMER-NAME END-PERFORM STOP RUN. ADD-CUSTOMER. ADD 1 TO CUSTOMER-COUNT MOVE CUSTOMER-COUNT TO I DISPLAY "Enter customer name: " ACCEPT CUSTOMER-NAME * Create new customer object INVOKE Customer "NEW" RETURNING CUSTOMER-OBJECT INVOKE CUSTOMER-OBJECT "setName" USING BY CONTENT CUSTOMER-NAME * Store in collection MOVE CUSTOMER-OBJECT TO CUSTOMER-LIST(I).
This example shows how to work with collections of objects using arrays of OBJECT-REFERENCE.
1234567891011121314151617181920212223242526272829303132* Safe object reference handling IDENTIFICATION DIVISION. PROGRAM-ID. SAFE-OBJECT-HANDLING. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-OBJECT OBJECT-REFERENCE INITIAL NULLS. 01 CUSTOMER-NAME PIC X(50). 01 OBJECT-EXISTS PIC X VALUE "N". PROCEDURE DIVISION. * Check if object exists before using it IF CUSTOMER-OBJECT = NULLS DISPLAY "No customer object exists" PERFORM CREATE-CUSTOMER END-IF * Safe method invocation IF CUSTOMER-OBJECT NOT = NULLS INVOKE CUSTOMER-OBJECT "getName" RETURNING CUSTOMER-NAME DISPLAY "Customer: " CUSTOMER-NAME ELSE DISPLAY "Error: Customer object is null" END-IF STOP RUN. CREATE-CUSTOMER. INVOKE Customer "NEW" RETURNING CUSTOMER-OBJECT INVOKE CUSTOMER-OBJECT "setName" USING BY CONTENT "Default Customer".
This example shows how to safely handle null object references to prevent runtime errors.
Following these best practices will help you use OBJECT-REFERENCE effectively in your COBOL applications.
Mistake | Problem | Solution |
---|---|---|
Not checking for null references | Runtime errors when invoking methods | Always check IF OBJECT-REF = NULLS before use |
Using wrong parameter types | Method invocation failures | Match parameter types exactly with method signature |
Forgetting to create objects | Null reference errors | Always use INVOKE class "NEW" to create objects |
Not handling exceptions | Program crashes on errors | Use ON EXCEPTION clauses in INVOKE statements |
Memory leaks | Excessive memory usage | Set object references to NULLS when done |
Action | Syntax | Example |
---|---|---|
Declare object reference | 01 var-name OBJECT-REFERENCE | 01 CUSTOMER-OBJ OBJECT-REFERENCE |
Create object instance | INVOKE class "NEW" RETURNING obj-ref | INVOKE Customer "NEW" RETURNING CUSTOMER-OBJ |
Invoke method | INVOKE obj-ref "method" USING params | INVOKE CUSTOMER-OBJ "setName" USING "John" |
Check for null | IF obj-ref = NULLS | IF CUSTOMER-OBJ = NULLS |
Set to null | MOVE NULLS TO obj-ref | MOVE NULLS TO CUSTOMER-OBJ |
Array of objects | 01 array OBJECT-REFERENCE OCCURS n | 01 CUSTOMER-LIST OBJECT-REFERENCE OCCURS 10 |
1. What is an OBJECT-REFERENCE in COBOL?
2. How do you declare an OBJECT-REFERENCE variable?
3. How do you create an object instance using OBJECT-REFERENCE?
4. What is polymorphism in the context of OBJECT-REFERENCE?
5. How do you check if an OBJECT-REFERENCE is null?
Understanding object-oriented programming concepts in COBOL.
Working with classes and objects in COBOL.
Understanding method invocation in object-oriented COBOL.
Understanding polymorphism in object-oriented COBOL.
Understanding inheritance in object-oriented COBOL.