Previous Topic Next topic Print topic


Constructors

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

constructor-specification

constructor-header Procedure Division Header chained-constructor

constructor-header

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

method-id new.
procedure division.
    set powerLevel to 0
end method.
 
method-id new.
procedure division using by value powerLevel as binary-long.
    set powerLevel to powerLevel
end method.

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.

Previous Topic Next topic Print topic