Saturday, August 8, 2020

Instance Constructor initializing attributes of class (CLASS 10)

Sample code for instance constructor.  

*&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL10
*& Insiatilize the instance constructor and it will automatically called whenever
*& object is created
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOABAP_CL10.
CLASS ZLCL_CUSTOMER DEFINITION.
  PUBLIC SECTION.
  METHODS CONSTRUCTOR,
            DISPLAY.
  PROTECTED SECTION.
  DATA KUNNR TYPE KUNNR,
         STCD1 TYPE STCD1.

ENDCLASS.

CLASS ZLCL_CUSTOMER IMPLEMENTATION.
  METHODDISPLAY.
    WRITE:/ KUNNR,
           STCD1.
  ENDMETHOD.
  METHOD CONSTRUCTOR.
    KUNNR 1209000.
    STCD1 'NOIDA'.
    ENDMETHOD.

 ENDCLASS.

 START-OF-SELECTION.
 DATA OB TYPE REF TO ZLCL_CUSTOMER.
 CREATE OBJECT OB.
 CALL METHOD OB->DISPLAY).