CONSTRUCTORS

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

*&---------------------------------------------------------------------*

*& Report ZDEMO_OBJCONSTRUCTORS


*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZDEMO_OBJCONSTRUCTORS.

*Constructors are special methods that cannot be called using CALL METHOD. Instead,
they are called automatically by the system to set the starting state of a new
object or class.

*Constructors are methods with a predefined name. To use them, you must declare
them explicitly in the class.

*There are two types of constructors - Instance constructors and Static


constructors.

*The Instance constructor of a class is the predefined instance method CONSTRUCTOR.

*The constructor's signature can have only importing parameters or exceptions.

*· Instance Constructor is a special instance method can access the


Instance Variables and Static Variables.
*· This method is used to set the default values in a class.
*· The Static constructor of a class is the predefined static method
CLASS_CONSTRUCTOR.

*· The constructor's signature cannot have importing parameters or


exceptions.
*· The static constructor is as special static method can access the static
variables.
*· These are used to set default values for the global attributes
irrespective of instances.
*· Multiple Instance and Static Constructors cannot be used in one class.

*
*Static constructor is declared by CLASS-METHODS CLASS_CONSTRUCTOR statement. Here
the naming convention must be followed. If we give any other name then that will
not be the static constructor. Now static constuctor is called
*1. before any other attributes and methods by using obj->var & obj->meth,
*2. before any other static attributes and methods by using cls=>var & cls=>meth,
*3. before creating of an object by using create object obj,
*4. before registering an event handler method by using set handler cls=>meth for
the object obj.
*
*Below is a program where we have defined a static data and a static constructor.
The static constructor is implemented in the method - endmethod portion. In the
start of selection we write the static attribute but in the output we can see the
static
*constructor function comes first.
*

*----------------------------------------------------------------------*
* CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS CLS DEFINITION.
PUBLIC SECTION.
CLASS-DATA V_TXT TYPE CHAR20 VALUE 'Class Constructor'.
CLASS-METHODS CLASS_CONSTRUCTOR.
ENDCLASS. "cls DEFINITION

*----------------------------------------------------------------------*
* CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS CLS IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
WRITE: / 'Today''s Date:', SY-DATUM DD/MM/YYYY.
ENDMETHOD. "class_constructor
ENDCLASS. "cls IMPLEMENTATION

START-OF-SELECTION.
WRITE: / CLS=>V_TXT.

You might also like