‎2006 Dec 07 6:41 PM
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
‎2006 Dec 07 6:54 PM
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
‎2006 Dec 11 10:55 AM
Check this sample as well. I hope it helped you.
REPORT ZOO.
----
CLASS counter DEFINITION
----
*
----
CLASS COUNTER DEFINITION.
PUBLIC SECTION.
METHODS INCREMENT.
PRIVATE SECTION.
DATA COUNT TYPE I.
ENDCLASS. "counter DEFINITION
----
CLASS counter IMPLEMENTATION
----
*
----
CLASS COUNTER IMPLEMENTATION.
METHOD INCREMENT.
COUNT = COUNT + 1.
WRITE:/ 'The Count is:', COUNT.
ENDMETHOD. "increment
ENDCLASS. "counter IMPLEMENTATION
DATA COUNT TYPE REF TO COUNTER.
START-OF-SELECTION.
CREATE OBJECT COUNT.
DO 5 TIMES.
CALL METHOD COUNT->INCREMENT.
ENDDO.
Regards,
Ramki.