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: 

Accessing the public attributes of singleton class in reports directly by object's reference

avinashd_m
Participant
0 Kudos

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,

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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.

4 REPLIES 4

Former Member
0 Kudos

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.

0 Kudos

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.



0 Kudos

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.

0 Kudos

thanks Manish, it worked.