When we create a variable in the public section of a class, we can use it within the methods of that class. Let’s say we assign it an initial value. Later, if we create another variable with the same name inside a method and assign it a new value, this new (local) value will be used within that method.
To access the original value from the class, we need to use the ME operator.
Example:
We first create a public variable v_txt with an initial value ‘Class Variable’. Then, inside a method, we declare another variable (local to the method) with the same name v_txt, but assign it a different value: ‘Method Variable’.
If we use v_txt directly in the method, it will reference the local variable, and we will get ‘Method Variable’.
If we use me->v_txt, we explicitly refer to the class attribute, and we get ‘Class Variable’.
Similarly, private and protected methods cannot be accessed directly from outside the class. However, we can call them from within another public method using the ME operator.
*&---------------------------------------------------------------------*
*& Report ZREP_LCLCLASS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zrep_lclclass.
" local class definition.
CLASS lcl_operator DEFINITION.
PUBLIC SECTION.
DATA : v_txt TYPE string VALUE 'Class Variable'.
METHODS: m_operator.
PROTECTED SECTION.
METHODS: m_protectedmethod.
PRIVATE SECTION.
METHODS: m_privatemethod.
ENDCLASS.
" local class implementation
CLASS lcl_operator IMPLEMENTATION.
METHOD m_protectedmethod.
WRITE :/ 'Hi from protected method'.
ENDMETHOD.
METHOD m_privatemethod.
WRITE :/ 'Hi from private method'.
ENDMETHOD.
METHOD m_operator.
DATA v_txt TYPE string VALUE 'Method Variable'.
WRITE: / v_txt,
/ me->v_txt.
me->m_protectedmethod( ). " protected method
me->m_privatemethod( ). " private method
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
" object for class.
DATA(lo_class) = NEW lcl_operator( ).
lo_class->m_operator( ).
OUTPUT :