2013 Jul 04 8:08 AM
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
2013 Jul 04 8:21 AM
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.
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
2013 Jul 04 8:18 AM
Hi Ramesh,
Seems a typing error.
You are setting OBJ variable and calling the OBJ1 method set the obj1->a = 30.
2013 Jul 04 8:51 AM
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.
2013 Jul 04 8:21 AM
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.
2013 Jul 04 8:52 AM
Hi Kumar,
same, I used for '40' but i am getting out why?
obj->b = '40'.
CALL METHOD obj1->disp.
2013 Jul 04 9:05 AM
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
2013 Jul 04 9:08 AM
Hi,
Hope You know the difference between static and instance ,
Your answer stored on that .Search in web for more information.
Thanks
Gourav.
2013 Jul 04 8:31 AM
Hi Ramesh,
Change the code as below,
obj1->a = '30'. " This value i am not getting in output.
CALL METHOD obj1->display.
Thanks,
Kannan
2013 Jul 04 8:50 AM
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