You are on page 1of 13

# Content

1 What is CONSTRUCTORS in SAP Class ?

2 Using Constructor method in SAP Class

3 Using CLASS CONSTRUCTOR in SAP Classes

What is CONSTRUCTORS in SAP


Class ?
Last Updated: November 8th 2013 by Ashok Kumar Reddy

What is a constructor method in SAP Classes ? and Uses of Constructor method


in OOABAP

+ -
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.

These methods are mainly used to set default values in classes.

CONSTRUCTORS are two types in SAP classes.

1. CONSTRUCTOR ( Instance Constructor).


2. CLASS CONSTRUCTOR (Static Constructor).

CONSTRUCTOR.

These methods can only have importing parameters, there will not be any exporting parameters.

The name of the CONSTRUCTOR method must be CONSTRUCTOR only.

CLASS CONSTRUCTOR (Also called as STATIC CONSTRUCTOR).

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.

Name of the CLASS CONSTRUCTOR must be CLASS_CONSTRUCTOR.

Learner Questions

No Questions by learners, be first one to ask ..!!

Using Constructor method in SAP


Class
Last Updated: November 8th 2013 by Ashok Kumar Reddy

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).

Go to SE24,provide class ZCL_SAPN_MATERIAL and click on change.


Go to Methods and provide method name as CONSTRUCTOR, enter

Once you press enter, you will see a symbol (Constructor symbol), it indicates that the method is a
constructor method.

Now put cursor on method name and click on parameters button.


Click on parameter button and add below parameter( what ever the parameters you add, they will
become importing parameters only).

Select attributes tab and add below attribute.

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.

Select parameters button and add below parameters.


Save, go back to methods and double click on method name GET_MATERIAL_DESCRIPTIONS,
add below code.

METHOD GET_MATERIAL_DESCRIPTIONS.

SELECT * FROM MAKT INTO EX_MAKT


WHERE MATNR = IM_MATNR
AND SPRAS = LANGUAGE. "LANGUAGE IS THE ATTRIBUTE DEFINED IN METHOD
ENDSELECT.
ENDMETHOD.

Using CONSTRUCTOR method with ABAP program


Go to SE38 and create a program ZSAPN_MATERIAL_DESCRIPTION and follow below steps.
Step1: Data declerations
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Declare class
DATA : WA_MAKT TYPE MAKT. "declare work area for makt
PARAMETERS P_MATNR TYPE MARA-MATNR. "material input

Step2: Create object for material class, the object can be created using ABAP patterns also.

To create object, click on pattern button.

A popup will open, select ABAP object patterns and enter.


One more pop up will come, select create object, provide instance name (We declared at the
declerations step LO_MATERIAL ), provide class name (our class name is
ZCL_SALN_MATERIALS) and enter.
Now the CONSTRUCTOR method is triggered, it will ask for a parameter IM_SPRAS (Which is
importing parameter of constructor method).It will set language key as English.
Finally call the method and get material descriptions.

The final code will be .


REPORT ZSAPN_GET_MATERIAL_DESCRIPTION.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS P_MATNR TYPE MARA-MATNR.
CREATE OBJECT LO_MATERIAL
EXPORTING
IM_SPRAS = 'E'.

START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.

WRITE : WA_MAKT-MATNR, WA_MAKT-MAKTX.

Testing the above program


Go to MAKT table and get a material number with multiple language descriptions.

Execute the program, provide the material no, you will get out put where language = 'E' only
(Constructor method handles this).

Learner Questions

No Questions by learners, be first one to ask ..!!


Using CLASS CONSTRUCTOR in
SAP Classes
Last Updated: November 8th 2013 by Ashok Kumar Reddy

What is a Class Constructor in OOABAP ?, Working with Class Constructor in


Object Oriented ABAP Programming

+ -
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)

Follow the below steps to create a class constructor.

Go to SE24, provide the class name ZCL_SAPN_MATERIALS, click on change.

Go to attributes tab and add an attribute to make default material type.

Go to methods and add a method name CLASS_CONSTRUCTOR as below.


We can not add any exporting and importing parameters to a CLASS_CONSTRUCTOR method.

Double click on the method CLASS_CONSTRUCTOR and add below code.

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

WRITE:/ 'Executed through Constructor', LO_MATERIAL->LANGUAGE . "Executed through


constructor method

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

No Questions by learners, be first one to ask ..!!

You might also like