OOPS ABAP- CONSTRUCTORS



CONSTRUCTORS

1. Constructors are unique type of methods.
2. Constructor methods are executed significantly whenever a object is created or
3. At every time  a class is loaded.
4. These methods are mainly used to set default values in a class. I.e. used to default values to attributes. (Ex V_LAND1 = ‘MX’)
5. These are declared only in public section
6. These methods cannot be called explicitly

                                           INSTANCE CONSTRUCTORS

1. These are special type of methods.
2. Constructor method is executed significantly whenever a object is created .
3. These methods are mainly used to set default values in a class.
4. The name of the constructor method is ‘constructor’.
5. These methods have only importing parameters.
6. There are no exporting parameters.

                                                 STATIC CONSTRUCTORS

        1. It is a type of constructor method.
        2. This method is executed naturally whenever a first call to the class is made.
        3. The first call can be through instance or class name.
                           Ex:  object1->get_my_details.
                                   Zcl_sample=>v_name2.
        4. Static constructor are used to set the default values globally .i.e., Irrespective of instances.
        5. The static constructors will not have any importing / exporting parameters .
        6. These are executed only once.
        7. The name of the static constructor is CLASS_ CONSTRUCTOR. 

   EXAMPLE ON CONSTRUCTOR

        Go to T code se24 and provide the class name.

 

        Define 2 Attributes




        Go to Methods Tab and create constructor and class constructor.






        To set default values  for Class constructor there will be no import/export parameters,
So we need to declare/write in source code (code icon).





To set default values for constructor here we need to use parameters, it won’t show export/import because constructor directly takes it as Importing.

 

Save, Activate it.

Calling constructor in se38