Example: passing conforming object-reference arguments from a COBOL client

The following example shows a way to make an object-reference argument in a COBOL client conform to the expected class of the corresponding formal parameter in an invoked method.

Class C defines a method M that has one parameter, a reference to an object of class java.lang.Object:


 . . .
 Class-id. C inherits Base.
 . . .
 Repository.
     Class Base       is "java.lang.Object"
     Class JavaObject is "java.lang.Object".
 Identification division.
 Factory.
 . . .
  Procedure Division.
   Identification Division.
   Method-id. "M".
   Data division.
   Linkage section.
   01 obj object reference JavaObject.
   Procedure Division using by value obj.
   . . .

To invoke method M, a COBOL client must pass an argument that is a reference to an object of class java.lang.Object. The client below defines a data item aString, which cannot be passed as an argument to M because aString is a reference to an object of class java.lang.String. The client first uses a SET statement to assign aString to a data item, anObj, that is a reference to an object of class java.lang.Object. (This SET statement is legal because java.lang.String is a subclass of java.lang.Object.) The client then passes anObj as the argument to M.


 . . .
 Repository.
     Class jstring    is "java.lang.String"
     Class JavaObject is "java.lang.Object".
 Data division.
 Local-storage section.
 01  aString object reference jstring.
 01  anObj   object reference JavaObject.
*
 Procedure division.
     . . . (statements here assign a value to aString)
     Set anObj to aString
     Invoke C "M"
       using by value anObj

Instead of using a SET statement to obtain anObj as a reference to an object of class java.lang.Object, the client could define aString and anObj with the REDEFINES clause as follows:


 . . .
 01  aString object reference jstring.
 01  anObj   redefines aString object reference JavaObject.

After the client assigns a value to data item aString (that is, a valid reference to an object of class java.lang.String), anObj can be passed as the argument to M. For an example of the use of the REDEFINES clause to obtain argument conformance, see the example referenced below.

Example: J2EE client written in COBOL

Related references  
INVOKE statement (Enterprise COBOL for z/OS® Language Reference)  
SET statement (Enterprise COBOL for z/OS Language Reference)  
REDEFINES clause (Enterprise COBOL for z/OS Language Reference)