Understand the difference between Constructors and Static/Class Constructors in ABAP OOPS.
Constructor is a special type of method which gets triggered during runtime. Whenever the object is created for class, the constructor gets triggered. It cannot be called using CALL METHOD.
There are two types of constructors: Instance Constructor and Class/Static Constructor
Instance Constructor
- Instance Constructor gets triggered when the class is instantiated.
- Instance Constructors can have importing parameters but no exporting parameters.
- Instance Constructors can raise exceptions.
Example 1:
The below program is an example to
understand how constructor works. The program has a class with constructor
method which prints some output whenever the object is created for class.
REPORT ztest_program. CLASS c1 DEFINITION. PUBLIC SECTION. METHODS: constructor. ENDCLASS. *&---------------------------------------------------------------------* *& Class (Implementation) C1 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* CLASS c1 IMPLEMENTATION. METHOD constructor. WRITE: 'Instance Constructor'. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA(lo_c1_obj) = NEW c1( ). " Class Instantiation
Instance Constructor
Example 2
The below program is an example for constructor with importing parameter. The program has a class with one instance constructor with importing parameter as num. If the input to constructor is less than 1 then exception is raised. The constructor gets triggered when the object is created for class C1.
REPORT ztest_program. CLASS c1 DEFINITION. PUBLIC SECTION. METHODS: constructor IMPORTING num TYPE i EXCEPTIONS e1. ENDCLASS. *&---------------------------------------------------------------------* *& Class (Implementation) C1 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* CLASS c1 IMPLEMENTATION. METHOD constructor. IF num LT 1. RAISE e1. ENDIF. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA: lo_obj TYPE REF TO c1. CREATE OBJECT lo_obj EXPORTING num = 0 EXCEPTIONS e1 = 1 OTHERS = 2. IF sy-subrc <> 0. WRITE: 'Exception Raised'. ENDIF.
Exception Raised
Static Constructor
(Class_Constructor)
Static or Class constructors are triggered in
following cases :
- When the object is instantiated for the class.
- When static attributes (class=>a) or methods (class=>method) are accessed from the class.
- Registering a static event handler method using SET HANDLER class=>meth for obj.
- Registering an event handler method of static event of the class.
In the below program the static attribute
‘num’ is accessed using the class name. Before accessing the static attribute
first the class constructor gets triggered. Thus, the class constructor always
gets triggered whenever the object is created for the class or any static
attributes are accessed.
Example 1:
REPORT ztest_program. CLASS c1 DEFINITION . PUBLIC SECTION. CLASS-DATA : num TYPE i VALUE 5. CLASS-METHODS :class_constructor. ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD class_constructor. WRITE:/ 'Class constructor'. ENDMETHOD. ENDCLASS. START-OF-SELECTION. WRITE:/ c1=>num.
Important : Irrespective
of the point at which the static attribute is accessed, the first thing that
gets triggered in the START-OF-SELECTION block is the class constructor method.
Consider below program where there is a
write statement which will print ‘Hello’ before accessing the static attribute
of a class containing the class constructor.
One would expect the output as:
Hello
Class constructor
5
But in this program the class constructor method
gets triggered first irrespective of the position in which the static attribute
is accessed.
Example 2
REPORT ztest_program. CLASS c1 DEFINITION . PUBLIC SECTION. CLASS-DATA : num TYPE i VALUE 5. CLASS-METHODS :class_constructor. ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD class_constructor. WRITE:/ 'Class constructor'. ENDMETHOD. ENDCLASS. START-OF-SELECTION. WRITE:/ 'Hello'. WRITE:/ c1=>num.
Output: