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

Values not getting in output?

former_member209120
Active Contributor
0 Likes
1,244

Values in the below program not getting in output why?

*General local class by using static and instance.
*Define the class
CLASS cl_lc DEFINITION.
   PUBLIC SECTION.
     DATA      : a TYPE i.                                  
     CLASS-DATA: b TYPE i.                                  
     METHODS display.
     CLASS-METHODS disp.
ENDCLASS.                    "CL_LC DEFINITION

*Implement the class
CLASS cl_lc IMPLEMENTATION.
   METHOD display.
     WRITE😕 'IT IS INSTANCE METHOD',
           / 'INSTANCE VARIABLE = ', a.
   ENDMETHOD.                    "DISPLAY
   METHOD disp.
     WRITE😕 'IT IS STATIC METHOD' COLOR 2,
           / 'STATIC VARIABLE = ', b COLOR 2.
   ENDMETHOD.                    "DISP
ENDCLASS.                    "CL_LC IMPLEMENTATION

*Create the object
DATA obj TYPE REF TO cl_lc.
DATA obj1 TYPE REF TO cl_lc.

START-OF-SELECTION.
   CREATE OBJECT obj.
   CREATE OBJECT obj1.
*Call the instance method
   obj->a = '10'.
   CALL METHOD obj->display .

*Call the static method.
   obj->b = '20'.
   CALL METHOD obj->disp.


*Call the instance method
  obj->a = '30'.    " This value i am not getting in output.
   CALL METHOD obj1->display.

*Call the static method.
   obj->b = '40'.
   CALL METHOD obj1->disp.

Out Put

IT IS INSTANCE METHOD

INSTANCE VARIABLE =          10

IT IS STATIC METHOD

STATIC VARIABLE =          20

IT IS INSTANCE METHOD

INSTANCE VARIABLE =          0    " Here 30 should come

IT IS STATIC METHOD

STATIC VARIABLE =          40


1 ACCEPTED SOLUTION
Read only

gouravkumar64
Active Contributor
0 Likes
1,195

Hi ramesh,

Replace your  code line ,

*Call the instance method

  obj->a = '30'.    " This value i am not getting in output.

   CALL METHOD obj1->display.

with

obj1->a = '30'.

Thanks

Gourav.

8 REPLIES 8
Read only

Former Member
0 Likes
1,195

Hi Ramesh,

Seems a typing error.

You are setting OBJ variable and calling the OBJ1 method set the obj1->a = 30.

Read only

0 Likes
1,195

Hi Yakub,

*Call the instance method
  obj->a = '30'.    " This value i am not getting in output.
   CALL METHOD obj1->display.

*Call the static method.
   obj->b = '40'.   " How i am getting this value can you explain why?
   CALL METHOD obj1->disp.   


Read only

gouravkumar64
Active Contributor
0 Likes
1,196

Hi ramesh,

Replace your  code line ,

*Call the instance method

  obj->a = '30'.    " This value i am not getting in output.

   CALL METHOD obj1->display.

with

obj1->a = '30'.

Thanks

Gourav.

Read only

0 Likes
1,195

Hi Kumar,

same, I used for  '40'  but i am getting out why?

obj->b = '40'.
   CALL METHOD obj1->disp.

Read only

0 Likes
1,195

Because "B" is static attribute of class and it will hold same value for all the objects of the class.

The statement CLASS-DATA declares a static attribute attr whose validity is not associated with instances of a class but with the class itself. All instances of the class and its subclasses access the same static attribute.

Regards

Read only

0 Likes
1,195

Hi,

Hope You know the difference between static and instance ,

Your answer stored on that .Search in web for more information.

Thanks

Gourav.

Read only

Former Member
0 Likes
1,195

Hi Ramesh,

Change the code as below,

   obj1->a = '30'.    " This value i am not getting in output.

   CALL METHOD obj1->display.

Thanks,

Kannan

Read only

former_member188827
Active Contributor
0 Likes
1,195

Instead of obj1 use obj:

*Call the instance method

  obj->a = '30'.    " This value i am not getting in output.

   CALL METHOD obj1->display.

Use:

*Call the instance method

  obj->a = '30'.    " This value i am not getting in output.

   CALL METHOD obj->display.

Regards