Creating Local singleton class in OOABAP

+ -

Below is the example of local singleton class in Object Oriented ABAP Programming .

REPORT ZSAPN_SINGLETON_CLASS.

DATA : IT_MARA TYPE TABLE OF MARA,
       WA_MARA TYPE MARA.
CLASS CL_SINGLETON DEFINITION CREATE PRIVATE. "create a private class
  PUBLIC SECTION.
    METHODS: GET_MARA. "actual method get mara
    CLASS-METHODS: INSTANTIATE RETURNING VALUE(LR_INST) TYPE REF TO CL_SINGLETON . "create a static method
  PRIVATE SECTION.
    CLASS-DATA: LR_INST TYPE REF TO CL_SINGLETON. "private variable
ENDCLASS.

DATA : LO_CLASS TYPE REF TO CL_SINGLETON.
LO_CLASS = CL_SINGLETON=>INSTANTIATE( ). "get instance of class
*CREATE OBJECT LO_CLASS.
LO_CLASS->GET_MARA( ). "get mara data


CLASS CL_SINGLETON IMPLEMENTATION.
  METHOD GET_MARA.
    SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
    LOOP AT IT_MARA INTO WA_MARA.
      WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
    ENDLOOP.
  ENDMETHOD.
  METHOD INSTANTIATE.
    IF LR_INST IS INITIAL.
      CREATE OBJECT LR_INST. "create object
    ENDIF.
  ENDMETHOD.

ENDCLASS.

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 14 People out of 14 think this lesson helpful


    Lesson Navigation

    ←Previous Chapter