Skip to Content
Technical Articles

IMPORTING parameter pass-by-value and pass-by-reference

Dear community, I recently read an ABAP source code. So far nothing special. That happens to me from time to time 😉 While reading, I noticed that someone possibly assumed that an object which was passed using the IMPORTING parameter cannot be changed at all.

Mmh, I wasn’t sure about the assumption because I know it a bit differently. So I’ve made a little example and put it along with my thoughts together in this blog. Just to check. Perhaps I’m wrong. Maybe it’s all clear to everyone, but maybe not. Then this blog is all the more valuable. So please take a look at this example first.

REPORT ztest.

CLASS lcl_object DEFINITION FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    DATA mv_color TYPE string.
    METHODS constructor.
ENDCLASS.

CLASS lcl_object IMPLEMENTATION.
  METHOD constructor.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_example DEFINITION FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    METHODS constructor.

    METHODS pass_by_reference
      IMPORTING
        io_object TYPE REF TO lcl_object.

    METHODS pass_by_value
      IMPORTING
        VALUE(io_object) TYPE REF TO lcl_object.
ENDCLASS.

CLASS lcl_example IMPLEMENTATION.
  METHOD constructor.
  ENDMETHOD.

  METHOD pass_by_reference.
    io_object->mv_color = 'green'.
  ENDMETHOD.

  METHOD pass_by_value.
    io_object->mv_color = 'blue'.
    CLEAR io_object.
    DATA(lo_object) = NEW lcl_object( ).
    lo_object->mv_color = 'yellow'.
    io_object = lo_object.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA(lo_example) = NEW lcl_example( ).
  DATA(lo_object) = NEW lcl_object( ).

  lo_object->mv_color = 'red'.
  WRITE: / lo_object->mv_color.

  lo_example->pass_by_reference( lo_object ).
  WRITE: / lo_object->mv_color.

  lo_example->pass_by_value( lo_object ).
  WRITE: / lo_object->mv_color.

Have a look at the method PASS_BY_REFERENCE. You cannot change the IMPORTING parameter itself. For example you cannot do a “CLEAR io_object “. But you can change the public attribute named MV_COLOR – even it’s an IMPORTING parameter passed by reference. It’s just the reference you cannot change but you can follow the reference to the object and manipulate that object.

Now have a look at the method PASS_BY_VALUE. You have access to the public attribute MV_COLOR and you can do a “CLEAR io_object”. It’s a local copy of the reference. Even more, you can create a new local object and move it to IO_OBJECT. All of this is possible

But not all is effective, however. If you run the example by F9 in Eclipse, the following color sequence is shown in console:

  1. red
  2. green
  3. blue

The explanation can be found in the ABAP help. See “pass by value” and “pass by reference” documentation. Another nice example with CHANGING parameters is show in this documentation.

 

Best regards, thanks for reading and please stay healthy

Michael

 

P.S.: Check our new “Virtual Wishing Well for Blogging“.

P.S.S.: Not tired of reading blogs? Read about this blog by Daniel Mieg.

10 Comments
You must be Logged on to comment or reply to a post.