Skip to end of metadata
Go to start of metadata

Usage of STATICS keyword:

           We are most prone to use the keyword DATA in ABAP for declaring variables which exist for the entire runtime of the program. But the local variables used in procedures exist as long as the procedure is running. There may be cases where we have to retain the value of the local variables beyond the runtime of the procedure. In such cases, we can use the STATICS keyword which declares a variable with the life time of the context of the main program but will be visible only within the procedure. 

Example: 

 DATA: f1 TYPE i VALUE 1,
      f2 TYPE i VALUE 1.

DO 3 TIMES.
  PERFORM scope.
ENDDO.

FORM scope.
  STATICS: f1 TYPE i VALUE 2,
           f2 TYPE i VALUE 2.

  ADD: 1 TO f1, 1 TO f2.
  WRITE: / f1, f2.

  PERFORM scope2.

ENDFORM.                    "scope

FORM scope2.
  ADD : 1 TO f1, 1 TO f2.
  WRITE: / f1, f2.
ENDFORM.                                                    "scope2 

Output:

                3           3

                2           2

                4           4

                3           3

                5           5

                4           4

                In this example, initially the value of F1 and F2 will be 1. When the execution comes inside the perform Scope, the value of F1 and F2 will be 2. After addition value of F1 and F2 will be 3. When the execution goes to the perform Scope2, the value of F1 and F2 will be 1 and after addition, it will be 2.

                Now for the second time when execution comes to the perform Scope, the value of F1 and F2 will be 3 as it is declared as STATICS. If we have declared the variables using the keyword DATA, then the value of F1 and F2 will be 2 now. After addition the value of F1 and F2 will be 4. Next when perform Scope2 is executed, the value of F1 and F2 will be 3. This repeats again for third time.

  • No labels