Gyan Factory

Gyan Factory
SAP Technical Project Support

Sunday, February 14, 2016

Constructor gets Triggered when Object is Created

Constructor gets triggered whenever the object is created but we need to call method to trigger the general method. Here we have a program where we have declared two public methods meth and constructor. Both the method have different operations. When we create the object of the class, the constructor method triggers its operation.


REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cl_construct DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct DEFINITION.
  PUBLIC SECTION.
    METHODS: m_meth, constructor.
ENDCLASS.                    "cl_construct DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_construct IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct IMPLEMENTATION.
  METHOD m_meth.
    WRITE: / 'General Method'.
  ENDMETHOD.                    "m_meth

  METHOD constructor.
    WRITE: / 'Constructor gets triggered object is created'.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "cl_construct IMPLEMENTATION

START-OF-SELECTION.  DATA obj TYPE REF TO cl_construct.
  CREATE OBJECT obj.

Below is the output:

No comments:

Post a Comment