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

instance of class

Former Member
0 Likes
742

hi all ,

i need to check wheather instance for a particular class is already created or not .

i will be passing the class name and i need the result.

Thanks and regards.

JK

1 ACCEPTED SOLUTION
Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
661

Hi Jayakumar,

I've alread answered this question on SDN Forum, you can see here que this .

You need control this in the class as the follow example.


*----------------------------------------------------------------------*
*       CLASS one DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION CREATE PRIVATE.
 
  PUBLIC SECTION.
 
    CLASS-METHODS create_objects EXPORTING obj_ref TYPE REF TO one.
 
    CLASS-METHODS clear_objects.
 
  PROTECTED SECTION.
 
  PRIVATE SECTION.
 
    CLASS-DATA obj_one TYPE REF TO one.
 
ENDCLASS.                    "one DEFINITION
 
 
*----------------------------------------------------------------------*
*       CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.
 
  METHOD create_objects.
 
    IF obj_one IS INITIAL.
      " Create a new instance
      CREATE OBJECT obj_one.
      MOVE obj_one TO obj_ref.
    ELSE.
      " Use an existing instance
      MOVE obj_one TO obj_ref.
    ENDIF.
 
  ENDMETHOD.                    "create_objects
 
  METHOD clear_objects.
 
    CLEAR obj_one.
 
  ENDMETHOD.                    "clear_objects
 
ENDCLASS.                    "one IMPLEMENTATION
 
DATA: obj1 TYPE REF TO one,
      obj2 TYPE REF TO one.
 
START-OF-SELECTION.
 
" Now you'll create the first instance for your class
  CALL METHOD one=>create_objects
    IMPORTING
      obj_ref                = obj1.
 
" Now you'll use an existing instance
  CALL METHOD one=>create_objects
    IMPORTING
      obj_ref                = obj2.
  
  CLEAR obj1.
  one=>clear_objects( ).
  
" Now you'll be able to create a new instance for your class
  CALL METHOD one=>create_objects
    IMPORTING
      obj_ref                = obj2.
 

I Hope it's usefull for you !

Regards.

Marcelo Ramos

Hi Jayakumar,

I've alread answered this question on SDN Forum, you can see here que this .

You need control this in the class as the follow example.


*----------------------------------------------------------------------*
*       CLASS one DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION CREATE PRIVATE.
 
  PUBLIC SECTION.
 
    CLASS-METHODS create_objects EXPORTING obj_ref TYPE REF TO one.
 
    CLASS-METHODS clear_objects.
 
  PROTECTED SECTION.
 
  PRIVATE SECTION.
 
    CLASS-DATA obj_one TYPE REF TO one.
 
ENDCLASS.                    "one DEFINITION
 
 
*----------------------------------------------------------------------*
*       CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.
 
  METHOD create_objects.
 
    IF obj_one IS INITIAL.
      " Create a new instance
      CREATE OBJECT obj_one.
      MOVE obj_one TO obj_ref.
    ELSE.
      " Use an existing instance
      MOVE obj_one TO obj_ref.
    ENDIF.
 
  ENDMETHOD.                    "create_objects
 
  METHOD clear_objects.
 
    CLEAR obj_one.
 
  ENDMETHOD.                    "clear_objects
 
ENDCLASS.                    "one IMPLEMENTATION
 
DATA: obj1 TYPE REF TO one,
      obj2 TYPE REF TO one.
 
START-OF-SELECTION.
 
" Now you'll create the first instance for your class
  CALL METHOD one=>create_objects
    IMPORTING
      obj_ref                = obj1.
 
" Now you'll use an existing instance
  CALL METHOD one=>create_objects
    IMPORTING
      obj_ref                = obj2.
  
  CLEAR obj1.
  one=>clear_objects( ).
  
" Now you'll be able to create a new instance for your class
  CALL METHOD one=>create_objects
    IMPORTING
      obj_ref                = obj2.
 

I Hope it's usefull for you !

Regards.

Marcelo Ramos

3 REPLIES 3
Read only

uwe_schieferstein
Active Contributor
0 Likes
661

Hello Jayakumar

I assume you want to check whether your reference variable hold an instance already. There are two ways available:


DATA:
  lo_myref    TYPE REF TO <class name / interface name>.


  IF ( lo_myref IS INITIAL ).

  ELSE.
"   we have an instance
  ENDIF.


" Since SAP release >= 6.20 a more specific statement is available:
  IF ( lo_myref IS BOUND ).
"   we have an instance
  ELSE.

  ENDIF.

Regards

Uwe

Read only

0 Likes
661

no its not like this ..

i have a constructor in the class.

so when i create instance for this class the constructor is fired .

in the constructor i need to check weather an instance has been already created or not .

if its created i will take the reference from that

if its not created then i will allow to create new instance.

Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
662

Hi Jayakumar,

I've alread answered this question on SDN Forum, you can see here que this .

You need control this in the class as the follow example.


*----------------------------------------------------------------------*
*       CLASS one DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION CREATE PRIVATE.
 
  PUBLIC SECTION.
 
    CLASS-METHODS create_objects EXPORTING obj_ref TYPE REF TO one.
 
    CLASS-METHODS clear_objects.
 
  PROTECTED SECTION.
 
  PRIVATE SECTION.
 
    CLASS-DATA obj_one TYPE REF TO one.
 
ENDCLASS.                    "one DEFINITION
 
 
*----------------------------------------------------------------------*
*       CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.
 
  METHOD create_objects.
 
    IF obj_one IS INITIAL.
      " Create a new instance
      CREATE OBJECT obj_one.
      MOVE obj_one TO obj_ref.
    ELSE.
      " Use an existing instance
      MOVE obj_one TO obj_ref.
    ENDIF.
 
  ENDMETHOD.                    "create_objects
 
  METHOD clear_objects.
 
    CLEAR obj_one.
 
  ENDMETHOD.                    "clear_objects
 
ENDCLASS.                    "one IMPLEMENTATION
 
DATA: obj1 TYPE REF TO one,
      obj2 TYPE REF TO one.
 
START-OF-SELECTION.
 
" Now you'll create the first instance for your class
  CALL METHOD one=>create_objects
    IMPORTING
      obj_ref                = obj1.
 
" Now you'll use an existing instance
  CALL METHOD one=>create_objects
    IMPORTING
      obj_ref                = obj2.
  
  CLEAR obj1.
  one=>clear_objects( ).
  
" Now you'll be able to create a new instance for your class
  CALL METHOD one=>create_objects
    IMPORTING
      obj_ref                = obj2.
 

I Hope it's usefull for you !

Regards.

Marcelo Ramos