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: 

New Object Instance is not unique

Former Member
0 Kudos

Hi,

I create 2 instances (object1, object2) of the same class and pass different parameters to the constructor.

The Constructor of the object1 is processed correctly.

But the constructor of object2 changes the values of object1.

This class is not final and not singleton and no reference assign.

Example:

data:

zcl1 TYPE REF TO z002_cl_01,

zcl2 TYPE REF TO z002_cl_01.

CREATE OBJECT zcl1

EXPORTING

iv_param1 = 'Z'.

CREATE OBJECT zcl2

EXPORTING

iv_param1 = 'N'.

Constructor:

..

mv_param1 = iv_param1. " Param1 saved to attribute

..

After ZCL2 is created, the attribute value of ZCL1->MV_PARAM1 changed from 'Z' to 'N'.

Why this? Please help

1 ACCEPTED SOLUTION

uwe_schieferstein
Active Contributor
0 Kudos

Hello Oleg

The only explanation that I have is that you defined attribute MV_PARAM1 as static instead of instance attribute.

Change from static to instance attribute and your coding works.

Regards

Uwe

4 REPLIES 4

uwe_schieferstein
Active Contributor
0 Kudos

Hello Oleg

The only explanation that I have is that you defined attribute MV_PARAM1 as static instead of instance attribute.

Change from static to instance attribute and your coding works.

Regards

Uwe

0 Kudos

Uwe,

you are the king ! It works now.

Habe Dich auch mit einem Extra-Punkt belohnt.

Grüsse

Oleg

0 Kudos

Hi there

you CAN also use an instance attribute

for example in a class i have zcl_alv_grid here's its constructor.




method constructor .
create object grid_container1
        exporting
*           container_name = 'CCONTAINER1'.
    container_name = cfname.
    create object  grid1
       exporting
          i_parent = grid_container1.
    set handler z_object->on_user_command for grid1.
    set handler z_object->on_toolbar for grid1.
    set handler z_object->handle_data_changed for grid1.
    set handler z_object->handle_data_changed_finished for grid1.
    set handler z_object->on_dubbelklik for grid1.
    set handler z_object->on_hotspot for grid1.
    call method grid1->register_edit_event
        exporting
           i_event_id = cl_gui_alv_grid=>mc_evt_enter.
  endmethod.
 

z_object is an import parameter

z_object type ref to zcl_alv_grid.

the relevant instance of z_object is created for example



 create object primary_grid
        exporting
                   z_object = primary_grid
                   cfname = 'CCONTAINER1'.
     assign primary_grid to <fs1>.
   endif.
   i_object = <fs1>.

so for using this instance i just call any method in the class

call method i_object->your_method.

Now for a NEW instance just do the same

create object secondary_grid

exporting

z_object = secondary_grid

cfname = 'CCONTAINER2'.

assign secondary_grid to <fs1>.

endif.

i_object1 = <fs1>.

Now call your method

call method i_object1->your_method.

Works a treat -- instance methods are often more useful than static methods in any case especially if you want to do a lot of dynamic calls.

Cheers

jimbo

Former Member
0 Kudos

A tip, that works pretty good and helps mixing up accesses to static attributes and instance attributes: Use ME->attribute to access instance attributes and class-name=>attribute" to address static attributes.