<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Object Instaniation in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004109#M709833</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You must check if object is initial, in this case you can creates an instance if the object is initial.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;See the example as Follow.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
...

DATA o_obj TYPE REF TO class.

START-OF-SELECTION.

...

  IF o_obj IS INITIAL.
    CREATE OBJECT o_obj.
  ENDIF.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Marcelo Ramos&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 31 Oct 2007 12:54:26 GMT</pubDate>
    <dc:creator>marcelo_ramos1</dc:creator>
    <dc:date>2007-10-31T12:54:26Z</dc:date>
    <item>
      <title>Object Instaniation</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004108#M709832</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a class  say A  and I instatiate the class (i.e create an Object).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I dont want another Object to be created. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 Oct 2007 12:16:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004108#M709832</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-10-31T12:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: Object Instaniation</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004109#M709833</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You must check if object is initial, in this case you can creates an instance if the object is initial.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;See the example as Follow.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
...

DATA o_obj TYPE REF TO class.

START-OF-SELECTION.

...

  IF o_obj IS INITIAL.
    CREATE OBJECT o_obj.
  ENDIF.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Marcelo Ramos&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 Oct 2007 12:54:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004109#M709833</guid>
      <dc:creator>marcelo_ramos1</dc:creator>
      <dc:date>2007-10-31T12:54:26Z</dc:date>
    </item>
    <item>
      <title>Re: Object Instaniation</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004110#M709834</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think "IS NOT BOUND" condition would be more appropriate. This checks if the object reference points to some valid object.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;hence you can use..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; if obj_ref is not bound.&lt;/P&gt;&lt;P&gt;      create object obj_ref.&lt;/P&gt;&lt;P&gt; endif.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 Oct 2007 13:21:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004110#M709834</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-10-31T13:21:58Z</dc:date>
    </item>
    <item>
      <title>Re: Object Instaniation</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004111#M709835</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;   1. I have a Class A.&lt;/P&gt;&lt;P&gt;    2. I create an object obj_ref1 for class A.&lt;/P&gt;&lt;P&gt;    3.I try creating obj_ref2 for the class A , there should be some validation that only one instance can be created .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The above is my requirement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 Oct 2007 13:51:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004111#M709835</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-10-31T13:51:43Z</dc:date>
    </item>
    <item>
      <title>Re: Object Instaniation</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004112#M709836</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Somya,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think that there is no such statement, so you can make a controller as follow:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
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 &amp;lt;o_obj&amp;gt;.  "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 &amp;lt;o_obj&amp;gt;.
  CLEAR v_instance.

  ...
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If the class A was deinited by you or if you can change it , you can do as follow:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
*----------------------------------------------------------------------*
*       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=&amp;gt;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=&amp;gt;create_objects
    IMPORTING
      obj_ref                = obj2
    EXCEPTIONS
      instance_alread_exists = 1.

  IF NOT sy-subrc IS INITIAL.
    "   you message
  ENDIF.

  CLEAR obj1.
  one=&amp;gt;clear_objects( ).
  
" Now you'll be able to create a new instance for your class
  CALL METHOD one=&amp;gt;create_objects
    IMPORTING
      obj_ref                = obj2
    EXCEPTIONS
      instance_alread_exists = 1.

  IF NOT sy-subrc IS INITIAL.
    "   you message
  ENDIF.  
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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 ). &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;b&amp;gt;Don't forget to close this thread when your question be answered&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Marcelo Ramos&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 Oct 2007 14:41:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/object-instaniation/m-p/3004112#M709836</guid>
      <dc:creator>marcelo_ramos1</dc:creator>
      <dc:date>2007-10-31T14:41:59Z</dc:date>
    </item>
  </channel>
</rss>

