Constructor of class A instantiating class B and vice versa

Hi folks,
I have two classes, namely ZCL_EMPLOYEE and ZCL_USER.
ZCL_EMPLOYEE has an attribute USER type ref to ZCL_USER, populated by a "create object" statement in the constructor method.
ZCL_USER has an attribute EMPLOYEE type ref to ZCL_EMPLOYEE, also populated by a "create object" statement in the constructor method.
Thus when I instantiate one of the classes, this inevitably results in an endless loop.
Is there any way to avoid this? I guess it must be possible to catch already instanciated objects and re-use them rather than instantiating all the time, but how?
Thanks a lot for your help,
Patrick

Hello Patrick
Since you need to create a couple of instance at the same time this is a task I would solve with the FACTORY pattern, e.g.:
Both class contain static factory methods and the instantiation is set private as suggested by Matthew, e.g.:
DATA:
  lo_user     TYPE REF TO zcl_user,
  lo_empl    TYPE REF TO zcl_employee.
  lo_user = zcl_user=>create( id_uname = <username> ).
  lo_empl = zcl_employee=>create( id_pernr = <personnel number> ).
CREATE method of ZCL_USER:
METHOD create. 
  CREATE OBJECT ro_instance    " of TYPE REF zcl_user
    EXPORTING
      id_uname = id_uname
*      io_employee =                     " optional parameter for employee instance      
ENDMETHOD.
CREATE method of ZCL_EMPLOYEE:
METHOD create.
  CREATE OBJECT ro_instance    " of TYPE REF zcl_employee
    EXPORTING
      id_pernr = id_pernr
*      io_user  =                            " optional parameter for user instance
ENDMETHOD.
CONSTRUCTOR method of ZCL_USER:
METHOD constructor.
  IF ( io_employee IS BOUND ).
    me->mo_employee = io_employee.
  ELSE.
    " get PERNR via infotype 0105
    CREATE OBJECT me->mo_employee
      EXPORTING
        id_pernr = <personnel number>
        io_user  = me.
  ENDIF.
ENDMETHOD.
CONSTRUCTOR method of ZCL_EMPLOYEE:
METHOD constructor.
  IF ( io_user IS BOUND ).
    me->mo_user = io_user.
  ELSE.
    " get uname via infotype 0105
    CREATE OBJECT me->mo_user
      EXPORTING
        id_uname = <username>
        io_employee  = me.
  ENDIF.
ENDMETHOD.
Final remark: If you are not yet aware of class CL_PT_EMPLOYEE you may want to have a look at my Wiki posting
[Unified Access to All HR Infotypes|https://wiki.sdn.sap.com/wiki/display/Snippets/UnifiedAccesstoAllHR+Infotypes]
Regards
  Uwe

Similar Messages

  • Object Creation Of One class into another and vice versa

    Hello Everyone,
    My name is Prashant Sharma from Jaipur [Rajasthan]
    Can u people help me
    How to create of one class into another and vice versa
    Eg,
    I am having class A and class B
    I want to create object of class A into class B and vice versa
    Plz help me out of this problem as soon as possible....

    Read this: [Creating Objects|http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html]
    Then, do one of these three:
    1. Create an instance of the class
    2. Extend the class
    3. Access methods within the class
    Try it out. Post the problem code if you're having trouble.

  • Is there a tx to view class corresponds to a BOL object and vice versa???

    Hello experts, is there any transaction or place where I can find the object BOL that corresponds to a CLASS and vice versa,
    For example for object ISUORDERITEM, where I can find this??
    Thanks in advance

    Hello Javier,
    Here is the link :
    SPRO --> IMG --> CRM > CRM Cross-Application Components> Generic Interaction Layer/Object Layer --> Basic Settings.
    How to find out the component set associated with the BSP UI component ??
    Go to the runtime repository of the component which you are working on, In the MODEL, the Component Set should be assigned.
    Once you get the component set, You can find the components associated with the component set.
    For each component there will be a GENIL Class associated in this IMG customizing.
    Eg : For BP , the class is CL_CRM_BUIL.
    You can find some infor at this Link: [Thread|Re: Rough Equivalent of PCUI MAC in CRM 2007 Web GUI.]
    Hope this helps,
    Regards, Vinay

  • Help needed XML to Internal table and vice versa

    Hello frnds, I need to convert Internal table to XML and Vice versa.
    Now I am able to most of the part except for this...
    the xml which I have to generate looks something like this...
    - <trade_dt>
    - <![CDATA[ 20111108000000:20111108235959
      ]]>
      </trade_dt>
    its a range I think
    And then the reponse which I get back the XML is like
    - <lockinfo>
    - <![CDATA[
    TRD_HEADER     1045     1          2
    ACT_CASHFLOW     1042               1
    TRD_TERM     1045               2
      ]]>
      </lockinfo>
    Is there any provision in class cl_ixml or class if_ixml_element to handle this part.....
    Edited by: Amit Sawant on Dec 28, 2011 3:51 PM

    Hello Amit,
    I would suggest you, to use the XSL-Transformations, which can be inbound used in ABAP.
    For example:
    DATA:
      l_xml       TYPE string,
      lt_flights  TYPE TABLE OF SFLIGHT.
    SELECT * FROM SFLIGHT INTO TABLE LT_FLIGHT.
    CALL TRANSFORMATION id
      SOURCE DATA = lt_flights
      RESULT XML l_xml.
    Now, you have a XML-String which is in the ABAP-XML Notation, which means, that ABAP can move this XML-Data back into an internal table/structure.
    For the backward, you have to use the following statement:
    CALL TRANSFORMATION id
      SOURCE XML l_xml
      RESULT DATA = lt_flights.
    As you will see, it is very easy. The Transformation "id" is just one example and build in. When you have to transform the data, or do not want to have the ASX-Notation in it, you should at least define your own transformations with the transaction XSLT_TOOL and use it similar to the transformation "id".
    Kind Regards,
    Hendrik

  • How to transfer texts from text control to table and vice versa

    Hi
    if anybody can give me link or any code giving how to transfer text from text control to table and vice versa, that will be of great help to me
    THanks in advance

    Rashmi,
    Use the methods GET_TEXT_AS_R3TABLE / SET_TEXT_AS_R3TABLE and then you will have to use the SAVE_TEXT to store in the database.
    The GET_TEXT_AS_R3TABLE method will give you back a internal in which text, in which text on the screen is existing. Similarly once you fetch the data from database using READ_TEXT, use the method SET_TEXT_AS_R3TABLE to put the text on the screen.
    These methods belong to class CL_GUI_TEXTEDIT class.
    Regards,
    Ravi
    note : Please mark all the helpful answers

  • How to call a Applet from a servlet and vice versa...?

    Hi all
    Can anyone help me how to call a applet from a servlet and vice versa. When the applet is called it should contact the database (oracle8i) and get the data. When i submit the applet form the data in the applet should be saved in the database.
    Thanks in advance
    Kamalakannan

    Sweep is correct about requestDispatcher being another approach for inter-servlet delegation; the only issue that i recall with this approach is that it defaults the method of the destination servlet to the one it was called from...for example, calling servlet2 from within servlet1.post() resulted in the dispatcher attempting to utilize servlet2.post() - i believe that i searched for a parameterize solution to no avail :( (ended up handling the request by placing a "fake" doPost() in servlet2 that simply called servlet2.doGet())
    however, if your application is functioning correctly on your pc/webserver then the problem may be external to servlet communication (e.g. client webserver's ports not configured or blocked, missing runtime classes, etc.)
    my suggestion would be to set aside the programmatic concerns for the moment - what is the response if you open a browser on a client's machine and access the URL in question (i.e. http://clientserver:port/stefanoServlet)? If it will not respond to access in this manner then it certainly won't when your application calls for it.
    It's possible that there is a coding error but, given the info supplied, i'd start examining the environment, first. Let us know if you have any luck with the test i recommended or not (please provide abundant detail). Or, if you've found the solution then you may want to post back with a quick blub so the next person knows how to escape the trap.
    D

  • What is the difference between action and workflow? How do I decide that I need an action in a transaction and not a workflow and vice versa?

    Dear Experts,
    I have few doubts and request your expert inputs to clarify my doubts.
    What is the difference between action and workflow? How do I decide that I need an action in a transaction and not a workflow and vice versa?
    Your earliest response is highly appreciated.
    Thanks,
    SMTP

    Hi SMTP,
    First of all, as I mentioned action is nothing but an executable work item which is designed in the workflow itself. In other words, Workflow is like your OOPS class. At run time, workflow instances are created just like objects of classes. Now, the steps designed in the workflow are called TASKS and the instances of tasks are called WORKITEMS. Now, the work items where any user action is required are called executable work items. ANd the work items where no user action is required (for example, sending an email in background) are called non-executable work items.
    Whether to go with development of workflow or not depends upon your business scenario. If your requirement is only to send an email and you find any BAdi or exit where in you can write your logic then there is no need of creating a workflow. If there is a defined business process with defined users and time lines, then you can go with the development of workflows.
    Rest, the below link will help you in understanding basic concepts of workflow :
    Why use SAP Workflow? | Insight Consulting Partners
    Regards,
    Richa

  • My family of four (2 iPhones, 2 iPods) all share the same iTunes account.  Many text messages sent to or from mine or my husband's phones are also received on my daughter's iPod, and vice versa. How do I fix this?

    How can I keep text messages sent from my or husband's phone from being received on daughter's iPod, and vice versa? We have five devices on one iTunes account - 2 iPhones, 2 iPods and an iPad.

    Hello Hrd1977,
    This is typically caused by both devices sharing an Apple ID for iMessage and FaceTime.  I would recommend signing out of iMessage on your daughter's device.  Go to Settings > Messages > Send & Receive, tap your Apple ID, and select Sign Out. Then sign in with the desired Apple ID.
    iOS: Troubleshooting FaceTime and iMessage activation
    http://support.apple.com/kb/TS4268
    Cheers,
    Allen

  • While inserting numeric data into a character datatype field and vice versa

    Sir,
        I have a very basic qiestion.Why am i not getting any error while inserting to say employees table a character type data to a numeric type field and vice versa?Iam using Oracle Database 10g Enterprise edition.

    Why am i not getting any error while inserting to say employees table a character type data to a numeric type field and vice versa?
    Oracle tries an automatic conversion, when possible. Post an example of a character type data inserted into a numeric column.

  • I just got 2 new iphone 4 (verizon) i have a issue, i have 2 itunes account 2 phone seperate names and emails. but when i update or sync my phone it copys my wife stuff on to my phone and vice versa.

    hello
    i need some help.
    well you see up above where i have 2 iphone 2 different names for the phones and 2 different accounts with 1 itunes on widows 7.
    my problem is when i update or sync my phone it grabs all her info and apps ect. and vice versa when she does it to. is there any way that i can stop this from happing again?
    1 more thing this happens just on her phone when she goes to download a app for some reason my email is on her phone, we have to sign out from her phone and sign back in and put her email in any thoughts on this?

    By far the simplest way to do this is create separate accounts on the computer for each person.  That way, you each log into your own separate account, sync your own device(s) to your own contacts/calendars/apps/music and so forth.

  • My nephew and I use the same iTunes account and when I add an app it is loaded on to his iPhone and vice versa... How do I make that stop?

    My nephew and I use the same iTunes account and when I add an app it is loaded on to his iPhone and vice versa... How do I make that stop? I have already unchecked the "Auto Update Apps" button. It happens with all purchases in the app store and on iTunes. HELP!

    I also think separate accounts is the way to go....if he is over 13yrs also remember if you do make an account for him and use your bank card all purchases will be charged to your card.

  • HT1766 Hi, I share an iTunes account with my wife. Sometimes she gets my iMessages and I don't and vice versa. Also if people face time me it goes to her phone. How can I stop this from happening?

    Hi, I share an iTunes account with my wife. Sometimes she gets my iMessages and I don't and vice versa. Also if people face time me it goes to her phone. How can I stop this from happening?

    You can share the same Apple ID in
    Settings > iTunes & App Store > Apple ID: here
    But for all other Apple services like:
    FaceTime, iMessage, iCloud, etc, one of you should use a different Apple ID.

  • Email sent on my iphone 5 does not show in the sent folder in mail on my macbook pro and vice versa especially since update to Yosemite

    I have an iphone 5 and a Macbook pro. Since upgrading to Yosemite, when I send an email from my yahoo email account or another email account linked to my website, on my iphone, the emails don't show in the sent folder in mail on my macbook pro and vice versa.
    I have deleted the accounts and re-created them on both my iphone and macbook pro but the samething keeps happening.
    When last I was at the istore, they were unable to sort it out and said I needed to book it in.
    Any suggestions that will prevent me from booking my macbook pro in are welcome.

    Hi,
    Are you saying you are trying to synch iMessages across two iPhones ?
    Or do you mean you are trying to get iMessage that were synced to one iPhone to show on another ?
    Whilst the first is possible - from the date you add the second iPhone the second  is not possible.
    10:11 PM      Saturday; January 5, 2013
    Please, if posting Logs, do not post any Log info after the line "Binary Images for iChat"
      iMac 2.5Ghz 5i 2011 (Mountain Lion 10.8.2)
     G4/1GhzDual MDD (Leopard 10.5.8)
     MacBookPro 2Gb (Snow Leopard 10.6.8)
     Mac OS X (10.6.8),
     Couple of iPhones and an iPad
    "Limit the Logs to the Bits above Binary Images."  No, Seriously

  • My partner and I share and iCloud account, we updated our software now whenever she gets a text message I see it on my phone and vice versa

    My partner and I share an iCloud account . We both have the iPhone 5. We recently updated the software to the iOS 7.2 and now whenever she gets a text message I receive and vice versa. This never happened before only happened since the update

    iCloud has nothing to do with messages (iCloud does include an email account, but not a messaging service).  If you both use the same AppleID for iMessage, then you are sharing the same messaging account.
    One of you needs to get an alternate AppleID, sigin out of iMessage, then sign in with the alternate AppleID.  That way you will each have your own private iMessage accounts.

  • How to sync my iPhone to my MacBook Pro and vice versa as it no longer seems to be doing?

    How to sync my iphone to my MacBook Pro and vice versa as it no longer seems to be doing? When I initially set up everything it worked fine; however, now my contacts are not syncing between devices?
    I have Icloud and even paid for ectra storage to see if this was the issue but it doesnt seems to be.  When I try and view devices on my itunes to sync it will not allow - blank option?
    If anyone could please help it would be really appreciated- thanks in advance:)

    I don't know how you got to your current position, but you might want to start by replacing your calendar with your MacBook Pro calendar if it is correct (if it isn't, skip this):  first disable calendar syncing on your phone (Settings>iCloud>Calendars set to OFF), then prior to syncing open iTunes on your computer and disable automatic syncing (under Preferences>Devices>Disable...from syncing automatically syncing).  Next connect your phone and set your sync settings on the Info tab to Syc Calendars with iCal, and below under Advanced choose "Replace Information on this iPhone" and check Calendars.  Now sync and this will hopefully replicate your MacBook calendar on your iPhone.
    Next, carefully review this to see that you have correctly set up iCloud on both your phone and your MacBook: http://www.apple.com/icloud/setup/, and that your meet the minimum system requirements (latest version of iOS on your phone, latest version of iTunes, OSX Lion 10.7.2 or later).

Maybe you are looking for

  • Ae crashing when opening project

    Since of yesterday AE started crashing when opening a project which i worked on a few weeks ago. We opened it on a PC, saved, with a new name. and it would open just fine. When reconnecting the files (AI-drawing) it crashed again. Renamed the drawing

  • KT4V crc errors

    hi there! i have crc errors in my system,i read that a bios update will fix the problem my system usually reboots when i ripping a dvd or moving files or i watch a dvd movie! suddenly the screen becomes blue and the system rebooting... i would like t

  • HO 6730b USB display problems (3 screen output)

    I need my laptop to have a three screen display output.  Primary Notebook, two external monitors secondary. I can get this configuration to work using a USB dsiplay link adapater (UGA-2K-A).  Other monitors on docking station either through VGA or DV

  • Bonjour... where did it go?

    Recently purchased the Airport Extreme wireless router. When I went to set up my USB printer, the directions said to enable Bonjour. For some reason, i can't locate Bonjour on my system, even after searching in Finder. Where did it go, and if it's go

  • Multi-Instrument, individual effects

    I've create a 16 channel multi-instrument template for one of my software instruments I bought. I can load and assign up to 16 instruments when using this instrument. ( I know I'm not limited to 16 if I want to layer instruments but I thought I'd kee