Professional Documents
Culture Documents
+ -
What is a constructor in a class ?
CONSTRUCTOR is a special type of method, it is executed automatically whenever a object is
created or instantiated.
CONSTRUCTOR.
These methods can only have importing parameters, there will not be any exporting parameters.
It is a type of constructor, this method will be executed automatically whenever a first call to the
class is made, the first call may be through instance or through class.
These CLASS CONSTRUCTORS are mainly used to set the default values globally i:e irrespective
of instances, these methods will not have any importing and exporting parameters.These methods
will be executed only once.
Learner Questions
Using CONSTRUCTOR method in SAP Classes to set global default value for
the call
+ -
This CONSTRUCTOR method is very useful in setting default value in a class, the below is the
example of using CONSTRUCTOR method in a SAP Class.Requirement: Display for material
description for a material, and depends upon language selected.
All material descriptions are stored in MAKT table, for a material there may be different descriptions
example for English one description, for German one description etc, we need to get description
based on the language, for this one we create a class method for reusablity(the same method can
be used in different programs to get descriptions based on the selected languages).
Once you press enter, you will see a symbol (Constructor symbol), it indicates that the method is a
constructor method.
Go to methods and double click on CONSTRUCTOR method and add below code.
Save and activate, go to methods tab and add one more method GET_MATERIAL_DESCRIPTIONS
to get material descriptions.
METHOD GET_MATERIAL_DESCRIPTIONS.
Step2: Create object for material class, the object can be created using ABAP patterns also.
START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
Execute the program, provide the material no, you will get out put where language = 'E' only
(Constructor method handles this).
Learner Questions
+ -
This is a type of CONSTRUCTOR, this method is executed whenever a first call to the class is
made, the call may be through instance or through class name.
These are also called as STATIC CONSTRUCTORS, the name must be CLASS_CONSTRUCTOR.
If you are coming directly for this lesson we highly recommend go through previous lesson Using
CONSTRUCTOR method in SAP classes for better understanding.
Example: We will add a class constructor to make default material type as 'FERT'(Finished Product)
METHOD CLASS_CONSTRUCTOR.
*Set default material type as FERT
MAT_TYPE = 'FERT'.
ENDMETHOD.
Here we take a small example to test this one.The below on is a simple example and explains you a
lot.
REPORT ZSAPN_CLASS_CONSTRUCTOR.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Declare class
CREATE OBJECT LO_MATERIAL "create object CONSTRUCTOR method will trigger
EXPORTING
IM_SPRAS = 'E'.
**When ever first call to a class is made class constructor will trigger
WRITE:/ 'Executed through class constructor', ZCL_SAPN_MATERIALS=>MAT_TYPE. "Executed
through Class Constructor
If we have both CONSTRUCTOR and CLASS_CONSTRUCTOR in our class, upon a class call
which will trigger first.....First CLASS_CONSTRUCTOR will be triggered.
Learner Questions