With the help of the previous Blog, you have established that objects belonging to a class are not created by the simple definition of the class. Neither does the instruction DATA: instance ref to CL_NAME creates an object. This instruction only creates a Reference, which in its initial state has the logical value INITIAL. Only with the instruction CREATE OBJECTinstance is the memory area for a new object requested from the system. The reference instance then refers to the object which has just been created. (The command CLEAR{{ instance. }}at this point means that the object, to which the reference variable refers, cannot be referenced. Therefore it can no longer be addressed in this program run. A Garbage Collector running in the background ensures that the object is removed from memory.
This separates object-oriented implementation from classic implementation. With the classic DATA instruction, main memory is reserved (which might never be used) and is pre-allocated the initial state of the relevant variable. With the "object-oriented" instruction DATA-x-TYPE-REF-TO, only the intention to create an object is expressed. The only storage space occupied is for an object reference.
In addition, every object has its own identity. Two references, which refer to objects, are only identical if they refer to the same object. Similarity between the attribute values of these objects is not the deciding factor. To get more idea about this see the following example.
REPORT Z_004_OBJECT_IDENTITY.
TYPES: TY_NAME(50) TYPE C.
*----------------------------------------------------------------
* CLASS CL_NAME DEFINITION
*----------------------------------------------------------------
CLASS CL_NAME DEFINITION.
PUBLIC SECTION.
METHODS: CONSTRUCTOR IMPORTING VALUE(IM_NAME) TYPE TY_NAME.
DATA: NAME TYPE TY_NAME.
ENDCLASS. "CL_NAME DEFINITION
*----------------------------------------------------------------
* CLASS CL_NAME IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_NAME IMPLEMENTATION.
METHOD CONSTRUCTOR.
NAME = IM_NAME.
ENDMETHOD. "CONSTRUCTOR
ENDCLASS. "CL_NAME IMPLEMENTATION
DATA: INSTANCE1 TYPE REF TO CL_NAME.
DATA: INSTANCE2 TYPE REF TO CL_NAME.
DATA: INSTANCE3 TYPE REF TO CL_NAME.
START-OF-SELECTION.
CREATE OBJECT INSTANCE1
EXPORTING
IM_NAME = 'VINO'.
CREATE OBJECT INSTANCE2
EXPORTING
IM_NAME = 'VINO'.
" Reference semantics !!!
IF INSTANCE1 = INSTANCE2.
WRITE 'This will never print, as instance1 refers to a
different object than instance2.'.
ENDIF.
" Attribute values are the same !!!
IF INSTANCE1->NAME = INSTANCE2->NAME.
WRITE 'Attribute values of instance1 and instace2 are same.'.
ENDIF.
INSTANCE3 = INSTANCE1.
IF INSTANCE3 = INSTANCE1.
WRITE 'Both references instance1 and instace3 refer to the
same object and are therefore identical.'.
ENDIF.
Program Output : 004
Attribute values of instance1 and instace2 are same.
Both references instance1 and instace3 refer to the same object and are therefore identical.
In the above program, the first IF-Query (IF }}INSTANCE1 = INSTANCE2{{.) via the object reference produces the value false, although both objects have the same attribute value "VINO". Note that in object-oriented languages the reference semantics apply for classes and their objects. Both objects have the attribute value {{NAME = }}VINO, but they are independent objects with their own identity. The references therefore refer to two different objects, although their attribute values are completely identical.
No comments:
Post a Comment