Understanding ‘me’ Variable in OO ABAP

Often times you might have come across me variable in OO ABAP today with an small example we will see how it is being used in the programs

*&---------------------------------------------------------------------*
*& Report y_class_basics
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
report y_class_basics.

class abap_basics definition.

public section.

data : num type i value '10'.
methods display.

endclass.

class abap_basics implementation.


method display.
data : num type i value '200'.
write : 'value = ', num.
write : 'value in me = ', me->num.
endmethod.

endclass.

start-of-selection.

DATA(lo_object) = NEW abap_basics( ).
lo_object->display( ).

So me always refers to Current calling Object of the class !!

" Select statement
SELECT * FROM mara INTO TABLE @DATA(lt_mara).

@ used in the Select to say where a statement or variable belongs

ls_t001 = VALUE #( bukrs = '1234').

In the example above, it refers to a data type of the variable to which the structure is assigned via VALUE

CL_GUI_TIMER class in ABAP

In modern abap we can use something as below which is clean and saves lof of time i.e. when we call method check with lo_object and the it returns it can be checked in one line instead of declaring variables

IF lo_object=>check( ) = abap_false.

Select within functions

" Select with functions
SELECT
  bukrs AS bukrs,
  CAST( kokfi AS CHAR( 20 ) ) AS text,
  CASE WHEN stceg = ' ' THEN '-'
       ELSE stceg
  END AS tax
 FROM t001
 WHERE bukrs LIKE 'A%'
   AND waers = @( to_upper( ld_waers ) )
 INTO TABLE @DATA(lt_bukrs).

There is a simple function module in the system for reading the current callstack. With SYSTEM_CALLSTACK you get the current call for the currently running code.

" Compare the two tables
CALL FUNCTION 'CTVB_COMPARE_TABLES'
  EXPORTING
    table_old  = lt_old
    table_new  = lt_new
    key_length = 14 
  IMPORTING
    table_del  = lt_del
    table_add  = lt_add
    table_mod  = lt_mod
    no_changes = ld_flag.

class cl_abap_random, which can generate random numbers.

" Create instance with factory
DATA(lo_rand) = cl_abap_random=>create( ).

" Random number from 1 till 6 (integer)
DATA(ld_num) = lo_rand->intinrange( low = 1 high = 6 ).

Leave a comment