Constructors

A constructor is a class method that is executed when the class is created.

constructor-specification

Specification of the constructors 
 constructor-header Procedure Division Header chained-constructor

constructor-header

Syntax of method constructors 
 constructor-signatureThe method signature enables you to specify passing parameters and returning items within the header of certain elements. You must omit the procedure division header within the method if you use this signature. Access Modifiers - SyntaxThe access modifiers of an item control the visibility (accessibility) of the item to other code. For example, you can call public methods from anywhere, whereas private methods can be called only from other methods within the same type. attribute-clause

Example

class-id SuperHero.
01 _powerLevel binary-long.

method-id new.
    set _powerLevel to 0
end method.

method-id new (powerLevel as binary-long).
    set _powerLevel to powerLevel
end method.

$if JVMGEN set
method-id Finalize override protected.
    *> JVM finalizer method. 
end method.
$else
method-id Finalize override protected.
    *> .NET finalizer method. 
end method.
$end
end class.

Further Information

If STATIC is not specified, the method is an instance constructor, and the code is executed when a class instance is created (see the NEW expression).

If STATIC is specified, the method is a static constructor, and is executed when the class is first loaded.

Neither instance constructors nor static constructors may specify a RETURNING item. Static constructors may not specify any parameters.

If you specify parameters in the constructor header, you must not include a procedure division header in the body of the constructor.