07-12-2014 11:05 AM
Hi,
I am new to ABAP. I know the importance and creation of singleton class. I have created one singleton class with one public member num1 and executed successfully.
Now in the report i have the following code
REPORT zsingleton_cl.
DATA: lo_obj TYPE REF TO zsingleton.
lo_obj = zsingleton=>get_instance_of( ).
IF lo_obj IS BOUND.
WRITE 'object bound'.
else.
WRITE 'object not bounded'.
ENDIF.
WRITE lo_obj->num1.
-------------------------------------------------------------------------------------------------------------
In report i am able to get the reference of the singleton class object and can access methods defined in the singleton class, but unable to access public attribute that is num1 of singleton class in report using the object of that class , in the last line above shown.
Is there any restriction in accessing public attributes of singleton class in report by its object, or is there any way to access public members of singleton class by its object except accessing attributed within the methods of that class.
Thanks,
07-12-2014 12:37 PM
I don't think so. I pasted your code and wrote zsingleton local class having private constructor and public static method get_instance_of . I am able to access the public attribute using your line.
07-12-2014 12:37 PM
I don't think so. I pasted your code and wrote zsingleton local class having private constructor and public static method get_instance_of . I am able to access the public attribute using your line.
07-12-2014 12:58 PM
Hello Manish,
this is my source code of singleton class , please have a look
class ZSINGLETON definition
public
create private .
public section.
data NUM1 type I value 23. "#EC NOTEXT .
class-methods GET_INSTANCE_OF
returning
value(OBJ) type ref to ZSINGLETON .
protected section.
private section.
class-data GREF_OBJ type ref to ZSINGLETON .
methods CONSTRUCTOR .
ENDCLASS.
CLASS ZSINGLETON IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZSINGLETON->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
method CONSTRUCTOR.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZSINGLETON=>GET_INSTANCE_OF
* +-------------------------------------------------------------------------------------------------+
* | [<-()] OBJ TYPE REF TO ZSINGLETON
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD get_instance_of.
IF gref_obj IS NOT BOUND.
CREATE OBJECT gref_obj.
obj = gref_obj.
ELSE.
obj = gref_obj.
ENDIF.
ENDMETHOD.
ENDCLASS.
07-12-2014 1:09 PM
I have pasted both your codes in single report and removed "public" from class "definition public create private" so that code gets compiled. Compilation was successful and report output is object bound<spaces>23.
07-14-2014 4:32 AM