Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Example of Self Reference

Former Member

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

1 ACCEPTED SOLUTION

former_member230674
Contributor

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.

3 REPLIES 3

Former Member

Hi Mohit,

Here "me->name" is the self reference.

When a class refereing itself (i.e. Here Server class has its name) it can be named as self reference.

To understand, you can use "My name is Mohit". In OOPs we use "me" for "My" or "mine".

"me" should be used inside the class only to refer its own attributes or methods.

Hope this will be useful !!!

Thanks,

Raj

former_member230674
Contributor

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 Kudos

>

> 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..

Not so. This is perfectly correct. The method acknowledge takes an object reference of type ref to "client". me fulfills that criterion.

matt