Create ABAP object dynamically

Is it possible to create objects dynamically?
so that the name of the class is set via parameters and also the variables and which class is the superclass?
something like:
fieldsymbols: <a> type any.
data: data_object type ref to object.
create new class type data_object.
data_object-name = 'dynamic'.
data_object-subclass = 'superclass'.
create object <a> like class 'dynamic'
<a>-(method of superclass)(   ) etc...
It is not ABAP code, but i hope you know what i mean?
Kindly hear from you!
Anton Pierhagen

You can use dynamic reference to instantiate your objects.
Like:
data: lo_ref type ref to object.
create object lo_Ref type ('ZCLASS').
call method lo_ref->('METH_1').
But problem in this design would be that your class & method name is hardcoded and it would be only checked at runtime, not at the design time. To overcome, this you can use the [Factory Method Design pattern|http://help-abap.zevolving.com/2011/10/abap-objects-design-patterns-factory-method/].
Regards,
Naimesh Patel

Similar Messages

  • Instantiate a ABAP Object Dynamically

    I would like to get some guidance on Dynamically instantiating objects within ABAP.
    I have worked with Java.  Within Java I am able to instantiate a Java object through the use Class.forName (see example below):
    className = "com.vz.it.cleansheet.eFine.db.query.WBRDMTWKCNDATABEAN";
    try {
         singleton = (Object) Class.forName(classname).newInstance();
         } catch (ClassNotFoundException cnf) {
                   System.out.println("SingletonRegistry - Couldn't find class "}
    Is there a similar procedure in ABAP OO to enable dynamically instantiating a ABAP object?
    I would appreciate any advice.
    Thanks,
    Rick

    You can instantiate an object dynamically,  here is a sample program.  Notice that we are creating an object passed on what class is named in the selection screen.
    report zrich_0001.
    *       CLASS lcl_car DEFINITION
    class lcl_car definition.
      public section.
        data: car type string.
    endclass.
    *       CLASS lcl_car IMPLEMENTATION
    class lcl_car implementation.
    endclass.
    *       CLASS lcl_truck DEFINITION
    class lcl_truck definition.
      public section.
        data: truck type string.
    endclass.
    *       CLASS lcl_truck IMPLEMENTATION
    class lcl_truck implementation.
    endclass.
    data: r  type ref to object.
    parameters: p_class(20) type c default 'LCL_TRUCK'.
    start-of-selection.
      create object r type (p_class).
      check sy-subrc = 0.
    Regards,
    Rich Heilman

  • How to Create View Object Dynamically

    Hi,
    I hav a requirement to create a view object dynamically.
    can anybody plz help me.
    Thanks
    Nan:)

    Hi,
    oracle.apps.fnd.framework.server.OAApplicationModuleImpl amImpl = (oracle.apps.fnd.framework.server.OAApplicationModuleImpl)AM;
    OAViewDef viewDef = AM.getOADBTransaction().createViewDef();
    viewDef.setSql(sql.toString());
    viewDef.setExpertMode(true);
    //viewDef.setViewObjectClass("oracle.apps.fnd.framework.server.OAViewObjectImpl");
    //viewDef.setViewRowClass("oracle.apps.fnd.framework.server.OAViewRowImpl");
    oracle.jbo.ViewObject oaviewobject=amImpl.createViewObject("TableVO",viewDef);
    if(oaviewobject !=null)
    TableBean.setViewUsageName("oaviewobject");
    TableBean.setViewAttributeName("TncId");
    Thanks
    Nani:)

  • ABAP OBJECTS: Dynamic Create object

    Hi folks!
    I have a problem... I need to create a dynamic type object with:
    <b>DATA: my_instance TYPE REF TO class1.
    CREATE OBJECT my_instance TYPE (class2).</b>
    <i>* where class1 is a superclass of class2.</i>
    When I do:
    <b>my_instance ?= m_parent.</b>
    <i>* where m_parent is an instance of class1</i>
    My problem is when I want to access to an attribute of the class2, the compiler says that it cannot find the attribute <i>(this is OK, because the attribute is only in the class2).</i>
    My question:
    Is there anyway to access to an atribute of second class when is not in the first class? (i don't want to create the attribute as an attribute of the first class).
    Thanx!!!!

    Hi David,
    When you do the debugging, you are dealing with run-time - i.e., the program is now running and you are just interrupting it at each statement to examine the program state. You will reach the point where the object is already created. That is why you can see all those attributes. But when you comiple, the program is not yet <i>running</i>, so the attributes will be unknown because of the dynamic type specification.
    I think you will have to redesign the program logic. As i had already said in my earlier post, it is not proper to have the attributes specified statically while the class itself is specified dynamically.
    Your situation is somewhat similar to -
    DATA ITAB TYPE TABLE OF SPFLI.
    PERFORM TEST TABLES ITAB.
    FORM TEST TABLES ITAB.
      LOOP AT ITAB.
        WRITE: / ITAB-CARRID.
      ENDLOOP.
    ENDFORM.
    Hope the point is clear.
    Regards,
    Anand Mandalika.

  • Dynamic documents in ABAP Objects (weblog)

    Hi SDNers,
    Do you want to implement the following features in ABAP Screens?
    1. Large font sizes and more colour options than traditional ABAP/4 (There are some limitations also)
    2. ICONS and pictures in different sizes
    3. Texts
    4. Links
    5. Pushbuttons
    6. Input fields
    7. Dropdown list boxes
    8. Tables with row span and with column span
    9. Tables with frames and without frames
    10. Tables with buttons, icons, pictures, input elements and texts in it.
    Then please read the below weblog to incorporate these features...
    <a href="/people/venkata.ramisetti/blog/2005/12/20/dynamic-documents-in-abap-objects">Dynamic Documents in ABAP Objects</a>
    Thanks,
    Ramakrishna

    one limitation which comes to my mind immediately is that you cannot create spool output of the dynamic document.
    Regards
    Raja

  • How to create a lock object dynamically ?

    Hi all,
    I have a report with  a selection screen having one parameter in which the user can put a table name(Custom table) and some other paremeters.Based on these parameters I need to delete records from table put on the selection screen.Before deeting I need to lock the table. As beforehand I dont know the table name how can I create lock object dynamically lock my table ?
    Please suggest some idea...

    Hi,
    check below link
    lock objects
    http://help.sap.com/erp2005_ehp_03/helpdata/EN/7b/f9813712f7434be10000009b38f8cf/frameset.htm
    Regards,
    Madhu

  • Poll: Development in ABAP Objects / Webdynpro vs. classical Dynpro

    Hey there ABAP developers,
    I just want to ask if you can give me one or two minutes of your attention for two poll questions.
    At the moment I´m writing my master thesis about the development of a monitoring tool in ABAP. One of my bigger chapters is about the decision, which programming paradigm should be used for new development projects in SAP. And another important one is about WebDynpro vs. classical Dynpros.
    Because of the fact, that I can´t create any polls in here, I just started this discussion and hope for many replies .
    It would be very nice if some of you could give me an answer to the following questions (only 2 ), so that I can maybe use the result of this poll in my master thesis, if there are enough responses.
    1. What percentage of new development projects are you developing in ABAP Objects? (Not to be considered small reports that just runs for only one time)
         A. 0 %
         B. less than 25%
         C. 25% - 49%
         D. 50% - 75%
         E. more than 75%
    2. Which GUI technology do you prefer?
         A. Classical Dynpro
         B. WebDynpro
         C. Business Server Pages (BSP)
         D. others (please mention)
    I want to thank you in advance for answering the questions,
    Best regards,
    Christoph

    Hi,
    Present SAP Implementation projects are very rare, maximum projects are support and up gradations only .
    If they want Implement the  SAP  newly  , defiantly they should creating ABAP Objects.
    Why Because  ABAP Objects are Object Oriented Concepts,  so,  for  future reference and re usability..etc .
    Now Come to the First Quetion.
    if it is implementation project   ABAP Objects are   25% - 49%.
    if it is Support project ABAP Objects are   25%
    Now Come to the Second Quetion.
    Depend upon Reqmnt, but Most of the Applications are Webdynpro .  i.e 70%.
    Remaining 30% All ( BSP and GUI ....Etc..)
    This is my opinion.
    Sambaiah.Paidipelli.

  • Create View Object programattically from XML input

    Hi,
    I have requirement to create view object from xml at run time in ADF application. is there any way to create view object dynamically in java program from xml?
    My requirement is to call a webservice and generate view object from the response of the web service call.
    Thanks

    I want to create view objects from different web services and the same will be available to the developer at the time of development. I don't want to create web service proxies for each of the web service. this will simplify the developers task to create proxy and data controls. the one thing developer has to do is select the service and base on the service the view objects will be created to develop pages accordingly.

  • ABAP Object Standards

    I have been charged with creating ABAP Object Standards for my company.  I have found naming conventions, but have not found any programming standards anywhere.  Does anyone know of any that I could use?
    Thanks
    Craig

    Hello Craig
    I assume you have found the following documentation about
    <a href="http://help.sap.com/saphelp_nw04/helpdata/en/92/c2b084bc1d11d2958700a0c94260a5/content.htm">Naming Conventions in ABAP Objects</a>
    ABAP Objects standards are, in principle, <b>OO standards</b> meaning that all OO-principles and OO designs/patterns that have proven to be useful should be standard in ABAP as well.
    I cannot forward you any standards in detail yet you may find more information in one of the books written by Horst Keller. Others sources may be good introductory books to Java (here I can recommend the books of the "Head First" series).
    Regards
      Uwe

  • ABAP OO, Creating object dynamically

    Hi everybody,
         I’m currently working on some Abap OO examples. Everything is working ok except for the following: Creating objects dynamically.
    This is what I’d like to do:
    Lets say we obtain a bunch of sales orders using a simple select statement and place them in an internal table. For each entry in our internal table, I’d like to generate a new sales order object. This salesorder object is a simple class with a constructor with an Id and some other attributes.
    When this is done, I’d like to append every object to a new object, say basket, so that this basket contains something like an internal table within each record a reference to the salesorder. Is this easy to do? I’m using the example that is described in the sap guide (create object pointer type (class_name) but it looks to me that you still have to declare the reference variables before you can create the object.
    Hmm, I think Java is better suited for a job like this.
    Hope you can help,
    Cheers and Best wishes
    Laurens Steffers
    The Netherlands.

    Hi,
    Its not all that difficult in ABAP as well. The following code is based on Chapter 5, Listing 5.20 from book ABAP Objects.
    What you need is an internal table of type <your sales object> e.g.
    DATA: BEGIN OF obj_sales_order
            sales_order_no LIKE <sales_order_no_type>,
            <...>,
            objref TYPE REF TO <SALES_ORDER_CLASS>
          END OF obj_sales_order,
          reftab LIKE SORTED TABLES OF obj_sales_order
                      WITH UNIQUE KEY sales_order_no
                                      <other key attributes>.
    Now all you need to do is to do a SELECT...ENDSELECT loop, and:
    SELECT ...
    obj_sales_order-sales_order_no = <selection result>
    obj_sales_order-<other key attr> = <selection result>
    READ TABLE reftab INTO obj_sales_order
                      FROM obj_sales_order.
    IF sy-subrc <> 0.
      CREATE OBJECT obj_sales_order-objref
             EXPORTING ...
             EXCEPTIONS ...
      IF sy-subrc <> 0.
        MESSAGE ...
        CONTINUE.
      ELSE.
        INSERT obj_sales_order INTO TABLE reftab.
      ENDIF.
    ENDIF.
    CALL METHOD obj_sales_order-objref-><method>.
    ENDSELECT.
    Hope this help.
    Regards

  • Creating and Binding View Objects dynamically : Oracle Jdeveloper 11g

    Hello,
    We are trying to create and bind view objects dynamically to adf data visualization components.
    The view object is a result of multiple tables.
    We are using Oracle JDeveloper 11g Technical Preview. ( can't upgrade to TP2 or TP3 now).
    We have found this : http://radio.weblogs.com/0118231/stories/2003/07/15/creatingUpdateableMultientityViewObjectDefinitionsDynamically.html on our search for the same.
    The sample application however, is in 10g , hence required migration.
    Also, it was a standalone application with the TestClient.java having a main() method.
    Our requirement is for Web Application; we use Adf+jsf .
    Guidance of any sort is very much appreciated.
    Thanks in advance.
    -Anil Golla

    Hi,
    there also exist a forum for JDeveloper 11: JDeveloper and OC4J 11g Technology Preview
    What you are trying todo is not trivial because you need to not only dynamically create the VO, you would also dynamically need to create the binding meta data for it (assuming you use ADF). Not sure if the API to modify the binding is public, so posting it on the JDeveloper 11 forum bears a glimpse of hope for an answer
    In JDeveloper 10.1.3 you can't do this
    Frank

  • How to create objects dynamically (with dynamic # of parameters)

    I need to create a set of objects based on a definition from an XML file (obtained from the server) and add them to my scene.
    I understand how to create the object using getDefinitionByName (and the limitations regarding classes needing to be referenced to be loaded into the SWF).
    But some objects require parameters for the constructor, and some don't. The XML can easily pass in the required parameter information, but I can't figure out how to create a new object with a dynamic set of arguments (something akin to using the Function.call(obj, argsArray) method.
    For example, I need something like this to work:
    var mc=new (getDefinitionByName(str) as Class).call(thisNewThing, argsArray)
    Currently this is as far as I can get:
    var mc=new (getDefinitionByName(str) as Class)(static, list, of, arguments)
    Thoughts?

    I think what Dave is asking is a bit different.
    He's wanting to know how to invoke the constructor of an object dynamically (when he only knows the # of constructor arguments at runtime).
    This class I know will do it but seems to be a hack:
    http://code.google.com/p/jsinterface/source/browse/trunk/source/core/aw/utils/ClassUtils.a s?spec=svn12&r=12
    See the 'call' method, which first counts the # of arguments then invokes one of 'n' construction methods based on the number of constructor args.
    I've yet to find a clean AS3 way of doing things ala 'call' though.
    -Corey

  • Dynamically create Value Objects from XML file

    Hi
    I want to create a value object from Xml file dynamically,like in the xml file i have the name of the variable and the datatype of the variable.is it possible do that,if so how.

    Read about apache's Digester tool. This is part of the Jakartha project. This tool helps in creating java objects from the XML files. I am not sure, if that is what u r looking for.

  • Calling a mime object in the WebDynPro ABAP code dynamically

    Hi ,
    Can anyone let me know the API to access the mime objects dynamically in the WD ABAP code. I have a text file in the WD ABAP component as a text file.
    I would like to pick the file with the help of API's and read the content of the file using Mime Object API's .
    Can anyone help me out in this.
    Best Regards
    Sid

    Hi,
    do the methods of CL_WD_WEB_ICON help?
    Regards, Heidi

  • Creat transaction (se93) for Globa class in ABAP Objects

    Hi experts,
           Can any one tell me how to
    creat transaction (se93) for Globa class(se24)  in ABAP Objects
    if ,please assest me how to do it or send me example docu on this
    best Answer will be rewarded
    regards
    fareedas

    hi
    se93->give your transaction ->enter create.
    check the radio button (Method of calss(OO class).press enter.
    in next screen.
    transaction text ;give your own description
    tick the check box OO transaction model.
    give the class name method name which u have created in se 24.which was activated successfully.it should be activated otherwise it will not work.
    under GUI SUPPORT.
    TICK the all 3 check boxes.
    SAP GUI  FOR HTML
                            JAVA
                           WINDOWS.
    then save.
    u should b saved in package(don't forget).
    it was working.
    i have already checked and did it succesfully.
    reward points
    if u have any queries let me know.
    kiran jagana

Maybe you are looking for

  • Creating a restricted key figure in BI7 reporting

    Hi I am creating a restricted key figure globally in BI7 query designer, whenever I try to restrict Calender Year /Month, an error message pops up showing: Runtime error '457' (This key is already associated with an element of this collection), when

  • Weblogic Server -- java.lang.OutOfMemoryError

    This is our production environment. We have deployed a Integration Application (EAR) on weblogic integration server. this is running in single server instance.there is no clustering (we have limitation from connection system to WLI) We get java.lang.

  • Query taking way too long

    Hello, Can somebody please help. I am wanting to change this query so that it does not take too long. How can I do it please? select count(*), t.tel_no, t.prospect_id, a.ACCOUNT_NO, a.account_title from tel_nos t, account a where t.tel_type = 'S' AND

  • QM View in material master

    Hi Experts,      I need to maintain the quality management view for the newly created materials and also have to assign the characteristics to all the materials, is there any way to do it at mass without using bdc or lsmw, if any method is there pls

  • Round up problem

    I face a problem here. How can i do the round up for this situation in Java? When 2550 is multiplied by 0.03 and then divided by 100. The answer i got is 0.765. When i do the round up, it seems that Java failed to display the correct round up to 0.77