Application Development and Automation 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: 
Read only

Instantiate a ABAP Object Dynamically

Former Member
0 Likes
2,220

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

2 REPLIES 2
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
760

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

Read only

0 Likes
760

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.