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: 

static attribute

Former Member
0 Kudos

Hi,

Can anyone explain me with simple example what is static attribute in ABAP Objects. Eventhough i read the same, I am confused regarding the same. Is it same as constant.

regards,

John

1 ACCEPTED SOLUTION

asik_shameem
Active Contributor
0 Kudos

Hi

Static variable is DEFINED by the key word CLASS-DATA.

Look at the following example.

REPORT  ztest_zero_01.

*-- Employee - Definition
CLASS employee DEFINITION.

  PUBLIC SECTION.
    TYPES:
      BEGIN OF t_employee,
        no  TYPE i,
        name TYPE string,
     END OF t_employee.

    METHODS: constructor
        IMPORTING im_employee_no TYPE i
                  im_employee_name TYPE string.

    METHODS:  display_employee.

    METHODS: display_no_of_employees.

  PRIVATE SECTION.
    DATA: g_employee TYPE t_employee.
*   Class data(Static) are global for all instances
    CLASS-DATA: g_no_of_employees TYPE i.  " STATIC ATTRIBUTE

ENDCLASS.


*--- LCL Employee - Implementation
CLASS employee IMPLEMENTATION.

* Where ever you create object this constructor will be called
  METHOD constructor.
    g_employee-no = im_employee_no.
    g_employee-name = im_employee_name.
    g_no_of_employees = g_no_of_employees + 1.

    " Increasing the variable by 1 Since its a static variable it RETAINS the previous value

  ENDMETHOD.

  METHOD display_employee.
    WRITE:/ 'Employee', g_employee-no, g_employee-name.
  ENDMETHOD.

  METHOD display_no_of_employees.
    WRITE: / 'Number of employees is:', g_no_of_employees.
  ENDMETHOD.

ENDCLASS.

***********************************************************
* R E P O R T
***********************************************************
DATA: g_employee1 TYPE REF TO employee,
      g_employee2 TYPE REF TO employee,
      g_employee3 TYPE REF TO employee.


START-OF-SELECTION.

  CREATE OBJECT g_employee1
    EXPORTING im_employee_no = 101
              im_employee_name = 'ASIK'.    " Here g_no_of_employees = 1
  CREATE OBJECT g_employee2
    EXPORTING im_employee_no = 102
              im_employee_name = 'THOMAS'.  " Here g_no_of_employees = 2
  CREATE OBJECT g_employee3
    EXPORTING im_employee_no = 103
              im_employee_name = 'PRINCE'.  " Here g_no_of_employees = 3

  CALL METHOD g_employee1->display_employee.
  CALL METHOD g_employee2->display_employee.
  CALL METHOD g_employee3->display_employee.

* since g_no_of_employees is a class-data (ie.STATIC data) where ever
* you create object this will be automatically increased. if u declared
* as normal data it cant be increased globally while creating object.

  CALL METHOD g_employee1->display_no_of_employees.

2 REPLIES 2

uwe_schieferstein
Active Contributor
0 Kudos

Hello John

In principle (public) static attributes can be accessed without having an instance of the class. The same is true for public constants, e.g.:


  IF ( cl_gui_alv_grid=>GUI_IS_RUNNING( ) = abap_true ).
   ...
  ENDIF.

  ld_ucomm = cl_gui_alv_grid=>MC_EVT_ENTER.
   ...

The only major difference I am aware of (perhaps Klaus Ziegler will correct me) is that you can change static attributes whereas constants have a fixed value.

And that is the main distinction when to use the one or the other (at least for me).

Regards

Uwe

asik_shameem
Active Contributor
0 Kudos

Hi

Static variable is DEFINED by the key word CLASS-DATA.

Look at the following example.

REPORT  ztest_zero_01.

*-- Employee - Definition
CLASS employee DEFINITION.

  PUBLIC SECTION.
    TYPES:
      BEGIN OF t_employee,
        no  TYPE i,
        name TYPE string,
     END OF t_employee.

    METHODS: constructor
        IMPORTING im_employee_no TYPE i
                  im_employee_name TYPE string.

    METHODS:  display_employee.

    METHODS: display_no_of_employees.

  PRIVATE SECTION.
    DATA: g_employee TYPE t_employee.
*   Class data(Static) are global for all instances
    CLASS-DATA: g_no_of_employees TYPE i.  " STATIC ATTRIBUTE

ENDCLASS.


*--- LCL Employee - Implementation
CLASS employee IMPLEMENTATION.

* Where ever you create object this constructor will be called
  METHOD constructor.
    g_employee-no = im_employee_no.
    g_employee-name = im_employee_name.
    g_no_of_employees = g_no_of_employees + 1.

    " Increasing the variable by 1 Since its a static variable it RETAINS the previous value

  ENDMETHOD.

  METHOD display_employee.
    WRITE:/ 'Employee', g_employee-no, g_employee-name.
  ENDMETHOD.

  METHOD display_no_of_employees.
    WRITE: / 'Number of employees is:', g_no_of_employees.
  ENDMETHOD.

ENDCLASS.

***********************************************************
* R E P O R T
***********************************************************
DATA: g_employee1 TYPE REF TO employee,
      g_employee2 TYPE REF TO employee,
      g_employee3 TYPE REF TO employee.


START-OF-SELECTION.

  CREATE OBJECT g_employee1
    EXPORTING im_employee_no = 101
              im_employee_name = 'ASIK'.    " Here g_no_of_employees = 1
  CREATE OBJECT g_employee2
    EXPORTING im_employee_no = 102
              im_employee_name = 'THOMAS'.  " Here g_no_of_employees = 2
  CREATE OBJECT g_employee3
    EXPORTING im_employee_no = 103
              im_employee_name = 'PRINCE'.  " Here g_no_of_employees = 3

  CALL METHOD g_employee1->display_employee.
  CALL METHOD g_employee2->display_employee.
  CALL METHOD g_employee3->display_employee.

* since g_no_of_employees is a class-data (ie.STATIC data) where ever
* you create object this will be automatically increased. if u declared
* as normal data it cant be increased globally while creating object.

  CALL METHOD g_employee1->display_no_of_employees.