COBOL 2002 added classes, interfaces, methods and user-defined functions, drawing on the object COBOL
already shipped by Micro Focus, IBM and Fujitsu. The source file shape changes: instead of four divisions
around one program, a class file is an IDENTIFICATION DIVISION with CLASS-ID, then a FACTORY (shared) part and an
OBJECT (per instance) part, each holding methods.
Class definition
CLASS-ID. Account INHERITS Base.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS Base AS "java.lang.Object"
CLASS Account AS "Account".
FACTORY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 open-count PIC 9(5) COMP-5 VALUE 0.
PROCEDURE DIVISION.
METHOD-ID. NewAccount.
DATA DIVISION.
LINKAGE SECTION.
01 result OBJECT REFERENCE Account.
PROCEDURE DIVISION RETURNING result.
ADD 1 TO open-count
INVOKE SELF "NEW" RETURNING result
END METHOD NewAccount.
END FACTORY.
OBJECT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 balance PIC S9(9)V99 COMP-3 VALUE 0.
PROCEDURE DIVISION.
METHOD-ID. Deposit.
DATA DIVISION.
LINKAGE SECTION.
01 amount PIC S9(9)V99 COMP-3.
PROCEDURE DIVISION USING amount.
ADD amount TO balance
END METHOD Deposit.
END OBJECT.
END CLASS Account.
| Element | Meaning |
|---|---|
CLASS-ID. name [AS "external"] [INHERITS parent…] [FINAL] [ABSTRACT] [USING type-params] | Starts a class. The 2002 standard has single and multiple inheritance, FINAL (cannot be extended) and parameterised classes through USING. Compilers differ in which they support. |
IMPLEMENTS interface… | Names the interfaces the class provides. |
FACTORY. … END FACTORY. | Class-level data and methods (like static). |
OBJECT. … END OBJECT. | Instance data and methods. |
METHOD-ID. name [AS "external"] [OVERRIDE] [FINAL] [PROPERTY | STATIC] | A method; ends with END METHOD name.. Properties generate GET and SET accessors. |
INTERFACE-ID. name [INHERITS …] | A set of method signatures with no bodies; END INTERFACE. |
REPOSITORY. | Names the classes, interfaces and functions used, and optionally their external names (CLASS Foo AS "com.example.Foo"). |
OBJECT REFERENCE [class | interface] | The usage of a variable holding an object. |
SELF, SUPER, NULL, EXCEPTION-OBJECT | The receiving object; the parent class’s version of a method; no object; the object of the exception being handled. |
Invoking
INVOKE Account "NewAccount" RETURNING acct
INVOKE acct "Deposit" USING 100.00
INVOKE acct "GetBalance" RETURNING bal
MOVE acct::"GetBalance" TO bal *> inline invocation (IBM)
SET acct TO NULL
INVOKE obj "method" USING … RETURNING …calls a method by name (a literal, or an item that holds the name).INVOKE class "NEW"creates an instance.- Arguments are passed by reference, by content or by value as for
CALL;RETURNINGnames the result. ON EXCEPTION/NOT ON EXCEPTIONandEND-INVOKEexist as forCALL.- The conditions
obj IS INSTANCE OF classandobj IS NULLtest an object.
User-defined functions and program types
FUNCTION-ID. name [AS "external"].starts a function: a program whosePROCEDURE DIVISION USING a b RETURNING rhas a result and which is called withFUNCTION name(a, b)from an expression. It ends withEND FUNCTION name.PROGRAM-ID. name [AS "external"] [IS] [COMMON | INITIAL | RECURSIVE | PROTOTYPE].The program attributes:COMMON(a nested program callable by siblings),INITIAL(state reset at each call),RECURSIVE(may call itself),PROTOTYPE(a signature only).ENTRY "name" USING …gives a program a second name with its own parameters (IBM, Micro Focus, GnuCOBOL).
Exceptions
The 2002 standard defines exception conditions (EC-SIZE, EC-BOUND, EC-I-O, EC-ARGUMENT…), checked after
>>TURN … CHECKING ON and handled with USE AFTER EXCEPTION CONDITION declaratives, RAISE EXCEPTION and RESUME
(see Statements). Object exceptions travel as objects of a class. Managed COBOL has
TRY/CATCH/FINALLY statements, below.
COBOL for .NET and the JVM
Micro Focus Visual COBOL (and its predecessors Net Express and Server Express) compiles COBOL to .NET assemblies and JVM classes; Fujitsu NetCOBOL for .NET did the same for the CLR. The language adds, beyond the standard:
CLASS-ID. Name AS "Namespace.Name";$set ilusing"System"orILUSING/JVMLANGdirectives to import namespaces.01 s TYPE String(or a framework class name) as the declaration of a typed variable;DECLARE s AS TYPE String, and inline declaration in the procedure division.TRY … CATCH ex AS TYPE Exception … FINALLY … END-TRYandRAISE/THROW.- Method call with parenthesis:
invoke s::ToUpper(),declare n = s::Length, properties by name. - Generics, delegates, lambdas, attributes,
PERFORMoverFOR EACH/PERFORM VARYING … OF collectionforms, operator overloading,usingstatements.
CLASS-ID. Hello.
METHOD-ID. Main STATIC.
PROCEDURE DIVISION.
DECLARE name AS STRING = "World"
TRY
DISPLAY "Hello, " & name::ToUpper()
CATCH ex AS TYPE Exception
DISPLAY ex::Message
END-TRY
END METHOD Main.
END CLASS Hello.
This syntax is documented by the compilers’ vendors (Micro Focus Visual COBOL language reference); I could not read it, so the list is from memory of that
product and may be incomplete. The viewer colours TRY, CATCH, FINALLY, THROW, DECLARE and END-TRY as keywords; other managed-only words (AS forms of generics, lambdas) get no special colour.