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.
CLASS lcl_object DEFINITION FINAL CREATE PUBLIC.
DATA mv_color TYPE string.
CLASS lcl_object IMPLEMENTATION.
CLASS lcl_example DEFINITION FINAL CREATE PUBLIC.
io_object TYPE REF TO lcl_object.
VALUE(io_object) TYPE REF TO lcl_object.
CLASS lcl_example IMPLEMENTATION.
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: