Application Development 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: 

Object problem

Former Member
0 Kudos

Hy !

Is there in SAP something similar to instanceof in Java ?

Thanks

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Check this. It is not directly built-in to the language, but you can acheive the same with a little coding.

report ztest.

*---------------------------------------------------------------------*
*       CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app definition.

  public section.
    data: field type string.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app implementation.


endclass.

data: o_ref type ref to cl_abap_classdescr,
      o_object type ref to object.

create object o_object type lcl_app.

o_ref ?= cl_abap_typedescr=>describe_by_name( 'LCL_APP' ).

if  o_ref->applies_to( o_object ) = 'X'.
  write / 'This Object is an instance of the class'.
else.
  write / 'This Object is NOT an instance of the class'.
endif.

Regards,

Rich Heilman

3 REPLIES 3

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Check this. It is not directly built-in to the language, but you can acheive the same with a little coding.

report ztest.

*---------------------------------------------------------------------*
*       CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app definition.

  public section.
    data: field type string.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app implementation.


endclass.

data: o_ref type ref to cl_abap_classdescr,
      o_object type ref to object.

create object o_object type lcl_app.

o_ref ?= cl_abap_typedescr=>describe_by_name( 'LCL_APP' ).

if  o_ref->applies_to( o_object ) = 'X'.
  write / 'This Object is an instance of the class'.
else.
  write / 'This Object is NOT an instance of the class'.
endif.

Regards,

Rich Heilman

uwe_schieferstein
Active Contributor
0 Kudos

Hello Ariana

Please have a look at the static method <b>CL_WDY_WB_REFLECTION_HELPER=>IS_INSTANCE_OF</b> (available on 6.40).

Regards

Uwe

0 Kudos

Please DONOT use class CL_WDY_WB_REFLECTION_HELPER. It is NOT released for public use. At least with higher levels of release or support package any use will violate package restrictions.

regards,

Klaus