Constructor is probably one of the most intricate concept in OOP which befuddles the novices. Confession; I had a difficult time understanding it.

Constructor is a special type of method used to define properties of an object. For example, Jack drives a Chevy Cobalt. Its color, transmission, battery type etc. are its properties. Car is the class and Jack’s Cobalt is the object. When the object is instantiated, the properties are not defined. Constructor is used to define these properties.

A constructor is a special kind of Public Method which has only importing parameters. In the implementation part the properties of car are populated.

CLASS <class_name> DEFINITION.

        PUBLIC SECTION.

METHODS CONSTRUCTOR IMPORTING <variable> <data_definition>.

ENDCLASS.

CLASS <class_name> IMPLEMENTATION.

            METHOD CONSTRUCTOR

             …

             ENDMETHOD .

ENDCLASS.

 

Self-Referencing – Within a class, if we want to refer the components of the class, we use self-referencing.

me-><attribute_name>

In the constructor, if importing value has to be assigned to itself, we use a code piece like –

me-><attribute_name> = <attribute_name>

The moment object is created, control goes to constructor method and updates values. Hence, while creating object, the importing values has to be passed.

CREATE OBJECT <object_name> EXPORTING           <parameter> = <value> .

Last thing, static constructor. It follows same philosophy as any static object. It abides by one rule – it is executed the first time an object is created or a static attribute or method is used. It does not have parameters. No parameters as well.

 

~S