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 Instaniation

Former Member
0 Kudos
155

Hi all,

I have a class say A and I instatiate the class (i.e create an Object).

I dont want another Object to be created.

How do i validate if there is already an object created . If there is one then while trying to create anothere object for that class , I should get an error.

Regards,

1 ACCEPTED SOLUTION

marcelo_ramos
Active Contributor
0 Kudos
91

Hi,

You must check if object is initial, in this case you can creates an instance if the object is initial.

See the example as Follow.


...

DATA o_obj TYPE REF TO class.

START-OF-SELECTION.

...

  IF o_obj IS INITIAL.
    CREATE OBJECT o_obj.
  ENDIF.

Regards.

Marcelo Ramos

4 REPLIES 4

marcelo_ramos
Active Contributor
0 Kudos
92

Hi,

You must check if object is initial, in this case you can creates an instance if the object is initial.

See the example as Follow.


...

DATA o_obj TYPE REF TO class.

START-OF-SELECTION.

...

  IF o_obj IS INITIAL.
    CREATE OBJECT o_obj.
  ENDIF.

Regards.

Marcelo Ramos

Former Member
0 Kudos
91

I think "IS NOT BOUND" condition would be more appropriate. This checks if the object reference points to some valid object.

hence you can use..

if obj_ref is not bound.

create object obj_ref.

endif.

Former Member
0 Kudos
91

Hi,

1. I have a Class A.

2. I create an object obj_ref1 for class A.

3.I try creating obj_ref2 for the class A , there should be some validation that only one instance can be created .

The above is my requirement.

Regards,

marcelo_ramos
Active Contributor
0 Kudos
91

Hi Somya,

I think that there is no such statement, so you can make a controller as follow:


DATA o_obj1 TYPE REF TO class.
DATA o_obj2 TYPE REF TO class.
 
DATA v_instance.

START-OF-SELECTION.
 
  IF v_instance IS INITIAL.
    CREATE OBJECT <o_obj>.  "Here you can work with o_obj1 or o_obj2
    MOVE 'X' to v_instance.
  ENDIF.

  ...
" If you need to clear( FREE or CLEAR ) the object you must clear the variable too.
  CLEAR <o_obj>.
  CLEAR v_instance.

  ...

If the class A was deinited by you or if you can change it , you can do as follow:


*----------------------------------------------------------------------*
*       CLASS one DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION CREATE PRIVATE.

  PUBLIC SECTION.

    CLASS-METHODS create_objects EXPORTING obj_ref TYPE REF TO one
                                 EXCEPTIONS instance_alread_exists.

    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 OBJECT obj_one.
      MOVE obj_one TO obj_ref.
    ELSE.
      RAISE instance_alread_exists.
    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
    EXCEPTIONS
      instance_alread_exists = 1.

  IF NOT sy-subrc IS INITIAL.
    "   you message
  ENDIF.

" Now a error will be triggerid
  CALL METHOD one=>create_objects
    IMPORTING
      obj_ref                = obj2
    EXCEPTIONS
      instance_alread_exists = 1.

  IF NOT sy-subrc IS INITIAL.
    "   you message
  ENDIF.

  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
    EXCEPTIONS
      instance_alread_exists = 1.

  IF NOT sy-subrc IS INITIAL.
    "   you message
  ENDIF.  

Obs.: You can use "IS INITIAL" to check if a object Contains value( a reference ) or you can use "IS BOUND" to check if a object Contains value( a reference ) and if this reference is valid( as Piyush Said ).

<b>Don't forget to close this thread when your question be answered</b>

Regards.

Marcelo Ramos