A constructor is a special method in object-oriented programming that is automatically invoked when an object of a class is created. Its primary purpose is to initialize the object, setting up the initial state or performing setup tasks required before the object can be used.
Real time example : Providing an email ID during account creation triggers a process that sets up your account with all the necessary details. Similarly, when an object is created in programming, the constructor sets up that object with the required data, making it ready to use.
Types of Constructors:
- Instance Constructor: Initializes an individual object.
- Static Constructor: Initializes static (class-level) data, which is shared among all instances of the class.
Instance Constructor: Initializes an instance of a class. It sets up the object with initial values or performs any setup tasks when the object is created. The instance constructor is a special method named CONSTRUCTOR
. It is automatically called when you create an object using CREATE OBJECT
.
Static Constructor: Initializes class-level (static) data before any instance of the class is created or any static methods are called. The static constructor is a special method named CLASS_CONSTRUCTOR
. It is automatically called once when the class is first loaded into memory.
CLASS CONS DEFINITION.
PUBLIC SECTION.
METHODS CONSTRUCTOR.
CLASS-METHODS CLASS_CONSTRUCTOR.
ENDCLASS.
CLASS CONS IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
WRITE: /'STATIC CONSTRUCTOR'.
ENDMETHOD.
METHOD CONSTRUCTOR.
WRITE: /'INSTANCE COSTRUCTOR'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(OBJ) = NEW CONS( ).
ULINE.
DATA(OBJ1) = NEW CONS( ).
This ABAP code demonstrates the use of both an instance constructor (
CONSTRUCTOR
) and a static constructor (CLASS_CONSTRUCTOR
).Class Definition:
CLASS CONS DEFINITION.
PUBLIC SECTION.
METHODS CONSTRUCTOR.
CLASS-METHODS CLASS_CONSTRUCTOR.
ENDCLASS
CLASS CONS DEFINITION: This defines a class named
CONS
.METHODS CONSTRUCTOR: Declares an instance constructor, which initializes individual objects of the class.
CLASS-METHODS CLASS_CONSTRUCTOR: Declares a static constructor, which initializes static (class-level) data when the class is first loaded.
Class Implementation:
CLASS CONS IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
WRITE: /'STATIC CONSTRUCTOR'.
ENDMETHOD.
METHOD CONSTRUCTOR.
WRITE: /'INSTANCE CONSTRUCTOR'.
ENDMETHOD.
ENDCLASS.
CLASS_CONSTRUCTOR Method: This is the static constructor. It prints
'STATIC CONSTRUCTOR'
when the classCONS
is loaded into memory for the first time.CONSTRUCTOR Method: This is the instance constructor. It prints
'INSTANCE CONSTRUCTOR'
every time a new object of the classCONS
is created.Program Execution:
START-OF-SELECTION.
DATA(OBJ) = NEW CONS( ).
ULINE.
DATA(OBJ1) = NEW CONS( ).
DATA(OBJ) = NEW CONS( ): This line creates a new object
OBJ
of the classCONS
. The static constructorCLASS_CONSTRUCTOR
is called first (only if it hasn't been called before). Then the instance constructorCONSTRUCTOR
is called, printing'INSTANCE CONSTRUCTOR'
.ULINE: This line prints a horizontal line (
----------------
) to separate the output for clarity.DATA(OBJ1) = NEW CONS( ): This creates a second object
OBJ1
of the classCONS
. The static constructor is not called again, but the instance constructor is called once more, printing'INSTANCE CONSTRUCTOR'
again.Output:
(
STATIC CONSTRUCTOR
): This is printed by the static constructor when the classCONS
is first accessed (whenOBJ
is created).(
INSTANCE CONSTRUCTOR
): Printed by the instance constructor whenOBJ
is created.(
--------------------
): Printed by theULINE
statement.(
INSTANCE CONSTRUCTOR
): Printed by the instance constructor whenOBJ1
is created.
IMPORTANT POINTS TO NOTE:
Static Constructor (
CLASS_CONSTRUCTOR
) runs only once when the class is first loaded.Instance Constructor (
CONSTRUCTOR
) runs every time a new object of the class is created.