2008 May 19 9:33 AM
Hi
I am not clear why do we need a self reference variable "me" to call the components of its own class? when it can be accessed directly as in the following examples?
In my first example below i can call the method display_name directly without creating a reference object or instance of the class within the class.
In the second example i am using the self refernce Write me->name to access the component of a class.
My question is why do we need "me" when i can use the components directly as shown in my first example.
CLASS egocentric DEFINITION.
PUBLIC SECTION.
DATA:
name(10) TYPE c READ-ONLY.
METHODS set_name.
METHODS display_name.
ENDCLASS. "egocentric DEFINITION
*---------------------------------------------------------------------*
* CLASS egocentric IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS egocentric IMPLEMENTATION.
METHOD set_name.
MOVE 'Abap Objects' TO name.
CALL method display_name.
ENDMETHOD. "write_name
METHOD display_name.
write: name.
ENDMETHOD. "display_name
ENDCLASS. "egocentric IMPLEMENTATION
*Global Data
DATA oref TYPE REF TO egocentric.
START-OF-SELECTION.
CREATE OBJECT oref.
CALL METHOD oref->set_name.
CLASS egocentric DEFINITION.
PUBLIC SECTION.
DATA:
name(10) TYPE c VALUE u2018Instructoru2019
READ-ONLY.
METHODS write_name.
ENDCLASS.
CLASS egocentric IMPLEMENTATION.
METHOD write_name.
WRITE me->name.
ENDMETHOD.
ENDCLASS.
*Global Data
DATA oref TYPE REF TO egocentric.
START-OF-SELECTION.
CREATE OBJECT oref.
CALL METHOD oref->write_name.
2008 May 19 10:14 AM
you can address an object itself by using the predefined reference variable ME within its instance methods. Generally, you do not need to use the prefix in such cases, but you may use it to improve readability. However, it is required when you want to show a distinction between local data objects and instance attributes with the same name.
2008 May 19 10:18 AM
As Micky said - sometimes you have to. I use "me" because it makes it clear that I'm accessing a attribute or a method or the class I'm in. One benefit of doing this is that the syntax checker will tell you if you've accidentally defined an instance attribute/method as a static.
matt
2008 May 19 10:26 AM
Hi Guys,
Thanks to both of your for your inputs.
Just one more question do we have good links on the SDN where i can learn more about ABAP Objects. I am new to ABAP Objects. I tried looking into e-learning but couldnt find a proper link for basics.
Cheers
2008 May 19 10:38 AM
You can go WIKI and search with ABAP Objects.
Or do the same in 'advanced search' and select a search area. You are bound to find something.
Or this link perhaps:
[abap objects|http://help.sap.com/saphelp_nw70/helpdata/EN/ce/b518b6513611d194a50000e8353423/content.htm]