Usins CONSTRUCTOR in local class

Using CONSTRUCTOR method in SAP local classes, CONSTRUCTOR in SAP ABAP Object Oriented Programming.

+ -

CONSTRUCTOR is a special type of a method which will trigger whenever a class object is instantiated (create object), mainly these constructors are used to set default values for the particaular instance.

CONSTRUCTOR name is always CONSTRUCTOR.Constructor is just similer to method, it only have importing parameters and no exporting parameters.

Follow below steps to create constructor method .

Define class

CLASS CL_CONSTRUCTOR_EXAMPLE DEFINITION. "class definition
  PUBLIC SECTION.
    DATA : LV_LANGUAGE TYPE SPRAS.
    METHODS : CONSTRUCTOR "Constructor method
              IMPORTING IM_SPRAS TYPE SPRAS.
    METHODS : GET_MATERIAL_DESCRIPTIONS "method to get material descriptions
              IMPORTING IM_MATNR TYPE MARA-MATNR
              EXPORTING EX_MAKT TYPE MAKT.

ENDCLASS.

Implement class

CLASS CL_CONSTRUCTOR_EXAMPLE IMPLEMENTATION. "class implementation
  METHOD: CONSTRUCTOR.
    LV_LANGUAGE = IM_SPRAS.
  ENDMETHOD.
  METHOD: GET_MATERIAL_DESCRIPTIONS.
    SELECT * FROM MAKT INTO EX_MAKT
      WHERE MATNR = IM_MATNR AND SPRAS = LV_LANGUAGE .
    ENDSELECT.
  ENDMETHOD.
ENDCLASS.               "CL_CONSTRUCTOR_EXAMPLE

Use the class

DATA LO_CLASS TYPE REF TO CL_CONSTRUCTOR_EXAMPLE. "declare class
DATA : WA_MAKT TYPE MAKT. "makt work area
PARAMETERS : P_MATNR TYPE MARA-MATNR. "input material number
PARAMETERS P_SPRAS TYPE MAKT-SPRAS. "input language to pass to constructor
START-OF-SELECTION.
  CREATE OBJECT LO_CLASS "create object for class, constructor method will trigger here
    EXPORTING
      IM_SPRAS = P_SPRAS.

  CALL METHOD LO_CLASS->GET_MATERIAL_DESCRIPTIONS "call method to get materiald escriptions
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MAKT  = WA_MAKT.


  WRITE :/ WA_MAKT-MATNR, WA_MAKT-MAKTX, WA_MAKT-SPRAS. "print data

Final code will be

REPORT ZSAPN_LOCAL_CLASS_CONSTRUCTOR.
CLASS CL_CONSTRUCTOR_EXAMPLE DEFINITION DEFERRED . "class is defined in next lines
DATA LO_CLASS TYPE REF TO CL_CONSTRUCTOR_EXAMPLE. "declare class
DATA : WA_MAKT TYPE MAKT. "makt work area
PARAMETERS : P_MATNR TYPE MARA-MATNR. "input material number
PARAMETERS P_SPRAS TYPE MAKT-SPRAS. "input language to pass to constructor


CLASS CL_CONSTRUCTOR_EXAMPLE DEFINITION. "class definition
  PUBLIC SECTION.
    DATA : LV_LANGUAGE TYPE SPRAS.
    METHODS : CONSTRUCTOR "Constructor method
              IMPORTING IM_SPRAS TYPE SPRAS.
    METHODS : GET_MATERIAL_DESCRIPTIONS "method to get material descriptions
              IMPORTING IM_MATNR TYPE MARA-MATNR
              EXPORTING EX_MAKT TYPE MAKT.

ENDCLASS.

START-OF-SELECTION.
  CREATE OBJECT LO_CLASS "create object for class, constructor method will trigger here
    EXPORTING
      IM_SPRAS = P_SPRAS.

  CALL METHOD LO_CLASS->GET_MATERIAL_DESCRIPTIONS "call method to get materiald escriptions
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MAKT  = WA_MAKT.


  WRITE :/ WA_MAKT-MATNR, WA_MAKT-MAKTX, WA_MAKT-SPRAS. "print data
CLASS CL_CONSTRUCTOR_EXAMPLE IMPLEMENTATION. "class implementation
  METHOD: CONSTRUCTOR.
    LV_LANGUAGE = IM_SPRAS.
  ENDMETHOD.
  METHOD: GET_MATERIAL_DESCRIPTIONS.
    SELECT * FROM MAKT INTO EX_MAKT
      WHERE MATNR = IM_MATNR AND SPRAS = LV_LANGUAGE .
    ENDSELECT.
  ENDMETHOD.
ENDCLASS.               "CL_CONSTRUCTOR_EXAMPLE

Learner Questions


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

    Please Sign in to ask a question

    Was this lesson helpful to you? Yes No 15 People out of 15 think this lesson helpful


    Lesson Navigation

    Events in Local Classes in SAP OOABAP← Previous Lesson
    Next Chapter →