Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Example of Self Reference

Hello experts,

I am totally new to ABAP Objects and was going through a book for the same to learn the concepts.

There is a section of Self Reference in this book. I could not understand this concept by myself. Could you please help me out to explain this thing.

PFB the code that is given in this book to explain Self Reference.

REPORT  z_self_reference.

*----------------------------------------------------------------------*
*       CLASS client DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS client DEFINITION.
  PUBLIC SECTION.
    DATA name(10) TYPE c VALUE 'Master' READ-ONLY.
    METHODS create_server.
ENDCLASS.                    "client DEFINITION

*----------------------------------------------------------------------*
*       CLASS server DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS server DEFINITION.
  PUBLIC SECTION.
    METHODS acknowledge
     IMPORTING creator TYPE REF TO client.
  PRIVATE SECTION.
    DATA name(10) TYPE c VALUE 'Servant'.
ENDCLASS.                    "server DEFINITION

*----------------------------------------------------------------------*
*       CLASS client IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS client IMPLEMENTATION.
  METHOD create_server.
    DATA server_ref TYPE REF TO server.
    CREATE OBJECT server_ref.
    CALL METHOD server_ref->acknowledge
      EXPORTING
        creator = me.
  ENDMETHOD.                    "create_server
ENDCLASS.                    "client IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS server IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS server IMPLEMENTATION.
  METHOD acknowledge.
    DATA name TYPE string.
    name = creator->name.
    WRITE: me->name, 'create by', name.
  ENDMETHOD.                    "acknowledge
ENDCLASS.                    "server IMPLEMENTATION

DATA client_ref TYPE REF TO client.

START-OF-SELECTION.
  CREATE OBJECT client_ref.
  CALL METHOD client_ref->create_server.

Thanks,

Mohit Goel.

Edited by: Matt on Apr 8, 2009 10:50 AM

Former Member
Former Member replied

Hi Mohit,

me is used to self reference the attributes and methods of a class within that class.In your program,

within client class implementation, you can use me->name and me->create_server to call the name variable and create_server method in the same class if required.Here me represents "client class".Similarly ,in Server class, you can use me->name and me->acknowledge to call the name variable and acknowledge method in the same class if required.

In your program ,

CALL METHOD server_ref->acknowledge

EXPORTING

creator = me.

is not correct.me is not used as variable . Its only a reference variable.it shoud used me->class attribute(name)/method.

I hope , you could understand the above explanation.

by

Prasad GVK.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question