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

CLASS-DATA in OO ABAP

Former Member
0 Likes
1,118

Hi,

I know the definition of CLASS-DATA in OO concept. I understand that it can be accessed by on CLASS-METHODS.

But what is the use of it ? Why do we need static data ?

Is this different fro STATICS we use in procedural ABAP.

Regards,

Rajesh.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
550

If there are multiple instances of the class, the data area is shared between all instances, which means when instance 1 of the class sets the value, then all other instances have the attribute with the same value, I believe that iit points to the same memory. Here is an example app, notice that then the first object is created, it sets the value, then when writing out the objects, both instances see the same value for NUM1 even though only the first object actually filled the value, so the second is looking at the same memory area because it is labeled as CLASS-DATA.



report zrich_0001.

*---------------------------------------------------------------------*
*       CLASS lcl_tst DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_tst definition.

  public section.

    class-data: num1 type n.

    data: num2 type n,
          num3 type n.

    methods: constructor importing im_num1 type n
                                   im_num2 type n
                                   im_num3 type n.

endclass.


*---------------------------------------------------------------------*
*       CLASS lcl_tst IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_tst implementation.

  method constructor.

    if not im_num1 is initial.
      num1 = im_num1.
    endif.

    if not im_num2 is initial.
      num2 = im_num2.
    endif.

    if not im_num3 is initial.
      num3 = im_num3.
    endif.

  endmethod.

endclass.


data: o_tst type ref to lcl_tst.
data: o_tst_list type table of ref to lcl_tst.
data: no_value type n.

start-of-selection.

  create object o_tst
        exporting
             im_num1 = '3'
             im_num2 = '4'
             im_num3 = '5'.
  append o_tst to o_tst_list.


  create object o_tst
     exporting
          im_num1 = no_value
          im_num2 = '6'
          im_num3 = '7'.
  append o_tst to o_tst_list.


  loop at o_tst_list into o_tst.
    write:/ o_tst->num1, o_tst->num2, o_tst->num3.
  endloop.

Regards,

Rich Heilman

2 REPLIES 2
Read only

Former Member
0 Likes
550

Hi,

REPORT YSUBOOPS17 .

CLASS c1 DEFINITION.

PUBLIC SECTION.

data : i_num type i value 5.

class-data :

s_num type i value 6 .

ENDCLASS.

CLASS c1 IMPLEMENTATION.

ENDCLASS.

START-OF-SELECTION.

DATA : oref1 TYPE REF TO c1 .

CREATE OBJECT : oref1.

write:/5 oref1->i_num.

write:/5 c1=>s_num .

write:/5 oref1->s_num.

Instance components exist separately in each instance (object) of the class and are referred using instance component selector using ‘’.

Static components only exist once per class and are valid for all instances of the class. They are declared with the CLASS- keywords

Static components can be used without even creating an instance of the class and are referred to using static component selector ‘ =>’ .

Regards,

keerthi

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
551

If there are multiple instances of the class, the data area is shared between all instances, which means when instance 1 of the class sets the value, then all other instances have the attribute with the same value, I believe that iit points to the same memory. Here is an example app, notice that then the first object is created, it sets the value, then when writing out the objects, both instances see the same value for NUM1 even though only the first object actually filled the value, so the second is looking at the same memory area because it is labeled as CLASS-DATA.



report zrich_0001.

*---------------------------------------------------------------------*
*       CLASS lcl_tst DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_tst definition.

  public section.

    class-data: num1 type n.

    data: num2 type n,
          num3 type n.

    methods: constructor importing im_num1 type n
                                   im_num2 type n
                                   im_num3 type n.

endclass.


*---------------------------------------------------------------------*
*       CLASS lcl_tst IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_tst implementation.

  method constructor.

    if not im_num1 is initial.
      num1 = im_num1.
    endif.

    if not im_num2 is initial.
      num2 = im_num2.
    endif.

    if not im_num3 is initial.
      num3 = im_num3.
    endif.

  endmethod.

endclass.


data: o_tst type ref to lcl_tst.
data: o_tst_list type table of ref to lcl_tst.
data: no_value type n.

start-of-selection.

  create object o_tst
        exporting
             im_num1 = '3'
             im_num2 = '4'
             im_num3 = '5'.
  append o_tst to o_tst_list.


  create object o_tst
     exporting
          im_num1 = no_value
          im_num2 = '6'
          im_num3 = '7'.
  append o_tst to o_tst_list.


  loop at o_tst_list into o_tst.
    write:/ o_tst->num1, o_tst->num2, o_tst->num3.
  endloop.

Regards,

Rich Heilman