Skip to Content
author's profile photo Former Member
Former Member

New Object Instance is not unique

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Jan 24, 2008 at 01:53 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      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

  • author's profile photo Former Member
    Former Member
    Posted on Jan 28, 2008 at 04:33 PM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.