SAP LIFETIME AND VISIBILITY



Get Example source ABAP code based on a different SAP table
  



Validity and Visibility
The context of a declarative statement in the source code of a program determines the validity and visibility of the declared component. In ABAP programs, there are three contexts in which data types and data objects can be declared:
Locally in Procedures Local data in procedures is valid while a procedure is being executed. It is created when the procedure is called and deleted when the procedure is exited. In addition to their local data and formal parameters, procedures see the global data of the compilation unit. Methods also 'see' the components of their class.
As Components of Classes Static attributes of classes exist for as long as the ABAP_ISESS of the ABAP program exists. Instance attributes of classes are bound to the lifetime of objects. They are created when an object is instantiated and deleted along with the object by the Garbage Collector. Classes also see the global data of the compilation unit.
Globally in the Compilation Unit Global program data exists for as long as the ABAP_ISESS of the ABAP program exists. It is created when the program is loaded in the ABAP_ISESS , and deleted from the ABAP_ISESS when the program is removed. An ABAP program usually only sees its own global data
BEGIN_SECTION VERSION 5 OUT (except for interface work areas ).
END_SECTION VERSION 5 OUT
All contexts see the program-independent type definitions of ABAP CDS, of the ABAP Dictionary and the type definitions and data declarations in the public visibility sections of global classes. Local declarations, however, hide global declarations with the same name. The following methods can be used to unhide these declarations:
In methods, components of classes can be addressed using an object component selector.
In statements in which a dynamic specification of a data type, a class, or an interface is possible, a global type can be addressed by specifying an absolute type name.
In addition to context-dependent visibility, the position of the declaration in the source text also plays a role. In a statement of an ABAP program only previous declarations are visible, regardless of the context. To ensure that the order in the program matches the context-dependent visibility, all global declarations of a program should be listed at the start of the source code, before the first processing block, and all local declarations should be listed immediately after the introduction of a procedure. The definition of an interface and the declaration part of a class are global declarations in this view. Note that reference variables that refer to a class can only be declared after the class has been declared. The implementation part, on the other hand, is one of the processing blocks.



Latest notes:

All data declared in the global declaration part of an ABAP program is global. Apart from the event block AT SELECTION-SCREEN, all data declared within event blocks and dialog modules is also global to the program. The data declared in the event blocks mentioned above is local to them. Data that is declared between closed processing blocks is also global in the program.
The ABAP system fields are located outside of the contexts listed above. They are hidden by global program data objects with the same names.
NON_V5_HINTS
The same is true for the obsolete structure screen.
ABAP_HINT_END

ABAP_EXAMPLE_VX
Declaration of a program-global field g_field, an instance attribute attr of a class, and a local field field of a method. Reference is made to the outer declarations in the inner declarations.
ABEXA 01071
ABAP_EXAMPLE_END

ABAP_EXAMPLE_V5
Declaration of an instance attribute attr of a class, and a local field field of a method. Reference is made to the outer declarations in the inner declaration.
ABEXA 01735
ABAP_EXAMPLE_END