Invoking methods (INVOKE)
In a Java™ client,
you can create object instances of classes that were implemented in
COBOL and invoke methods on those objects using standard Java syntax. In a COBOL client, you can invoke
methods that are defined in Java or
COBOL classes by coding the INVOKE
statement.
About this task
Invoke Account "createAccount"
using by value 123456
returning anAccount
Invoke anAccount "credit" using by value 500.
The first example INVOKE
statement
above uses the class-name Account to invoke a method called createAccount.
This method must be either defined or inherited in the Account class,
and must be one of the following types:
- A Java static method
- A COBOL factory method
The phrase using by value 123456
indicates
that 123456 is an input argument to the method, and is passed by value.
The input argument 123456 and the returned data item anAccount must
conform to the definition of the formal parameters and return type,
respectively, of the (possibly overloaded) createAccount method.
The
second INVOKE
statement uses the returned object
reference anAccount to invoke the instance method credit
,
which is defined in the Account class. The input argument 500 must
conform to the definition of the formal parameters of the (possibly
overloaded) credit
method.
Code the name of the method to be invoked either as a literal or as an identifier whose value at run time matches the method-name in the signature of the target method. The method-name must be an alphanumeric or national literal or a category alphabetic, alphanumeric, or national data item, and is interpreted in a case-sensitive manner.
When
you code an INVOKE
statement using an object reference
(as in the second example statement above), the statement begins with
one of the following two forms:
Invoke objRef "literal-name" . . .
Invoke objRef identifier-name . . .
When the method-name is an identifier, you must define
the object reference (objRef) as USAGE OBJECT REFERENCE
with
no specified type, that is, as a universal object reference.
If an
invoked method is not supported in the class to which the object reference
refers, a severity-3 Language Environment® condition is raised at run time unless you code
the ON EXCEPTION
phrase in the INVOKE
statement.
You
can use the optional scope terminator END-INVOKE
with
the INVOKE
statement.
The INVOKE
statement
does not set the RETURN-CODE
special register.
USING phrase for passing arguments
RETURNING phrase for obtaining a returned value
PROCEDURE DIVISION for defining a class instance method
Coding interoperable data types in OO COBOL and Java
Invoking overridden superclass methods
Invoking factory or static methods