ME in Method

ME in Method


When we declare a variable of any type in public section of a class then we can use it in the implementation. Let us suppose we declare a variable of any type with an initial value in public section. Then we declare the variable again inside a method initiating with a different value. Now if we write the variable inside the method, the system will print the changed value not the previous one. To reflect the previous value of the variable we have to use ME operator.

In the following example we have declared a public variable v_txt and initiate with a value. Then we have declared the same variable again inside the method but instantiated with different value. Inside the method we are writing that variable with ME operator and get the previously initiated value. By declaring directly we are getting the changed value.

REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS cl_me DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_me DEFINITION.
  PUBLIC SECTION.
    DATA v_txt TYPE char40 VALUE 'Class Attribute'.
    METHODS m_me.
ENDCLASS.                    "cl_me DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_me IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_me IMPLEMENTATION.
  METHOD m_me.
    DATA v_txt TYPE char40 VALUE 'Method Attribute'.

    WRITE: / me->v_txt,
           / v_txt.
  ENDMETHOD.                    "m_me
ENDCLASS.                    "cl_me IMPLEMENTATION

START-OF-SELECTION.  DATA obj TYPE REF TO cl_me.
  CREATE OBJECT obj.
  CALL METHOD obj->m_me.

The program generates the following output.

Share this

Related Posts