SAP DECLARATION VARIABLES - Guide
Get Example source ABAP code based on a different SAP table
Declaration of Variables
ABAP_BACKGROUND
Variables can be declared in the following contexts:
Variables that are declared within most of the event blocks or dialog modules as well as between completed processing blocks also belong to the global declaration part of a program, but violate the rule
Program-local variables that are declared in the global declaration part of a program are generally referred to as
ABAP_RULE
Do not declare variables in the global declaration part of a program. Variables may only be declared as attributes of classes and interfaces or locally in methods.
ABAP_DETAILS
This rule is directly derived from the basic rule
Furthermore, the rule
Except for the following exception, you should not declare any global variables in a new ABAP program. They indicate a poor programming style that disregards proven concepts such as task sharing and encapsulation. If you need to access the same data of a program from multiple local classes and interfaces, you must create them in an appropriate visibility section of a local class or an interface. These can also be local classes or interfaces that contain nothing but such attributes.
Latest notes:
The above rule also applies to the declaration of
ABAP_HINT_END
Exception
If classic dynpros and selections screens are still used instead of
In these cases, you have to ensure the maximum possible
ABAP_EXAMPLE_BAD
The following source code shows the top include of a function pool for document display. In addition to the required interface work area, which is declared with
TABLES document_structure.
DATA: g_language TYPE sy-langu,
g_display_mode TYPE ...
...
CLASS screen_handler DEFINITION.
PUBLIC SECTION.
...>
ABAP_EXAMPLE_END
ABAP_EXAMPLE_GOOD
The following source code shows an improved example. The previously global variables are encapsulated in a class that is specifically provided for the state of the display, and can be addressed using
TABLES document_structure.
CLASS display_status DEFINITION.
PUBLIC SECTION.
CLASS-DATA: language TYPE sy-langu,
display_mode TYPE ...
...
ENDCLASS.
CLASS screen_handler DEFINITION.
PUBLIC SECTION.
...>
ABAP_EXAMPLE_END