What is Variable?
Variable are named data objects that refers to the memory location allocated during the program execution for processing. Variable contains data and act as a referring name for the data.
We can modify data, change data, clear data or apply operations on the data with help of the variable. Each variable should declare with a data type along with size in ABAP.
A variable must be declared before using it. A variable memory always allocated before the program execution and destroys once the program execution completed. No permanent memory allocated to a variable.
Now let us see the variable declaration syntax below –
Syntax –
DATA: Variable-name TYPE data-type [VALUE data-value].
- Variable-name – Specifies the variable name. Variable name length can be up to 30 characters.
- Data-type – Specifies the data type. We have discussed data types in the previous chapter.
- Data-value – Value that initially assigned to the variable during the declaration.
Variable Naming Convention –
Variable naming convention standard is project dependentant and may vary from project to project. Below is one of the standard ABAP variable naming convention –
Syntax –
{Visibility_character}{data_type_character}_{Remaining_field_name}
Below table specifies the list of options available for visibility_character.
Below table specifies the list of options available for data_type_character.
Remaining_field_name can be any combination of alpha-numeric characters of developer choice based on the variable usage.
Variable types –
ABAP supports 5 different variable types based on the structure and durability in the program. Those are –
Variable Types | Description |
---|---|
Local variables | Local variables are very local to the program or function or include etc,. |
Static variables | Static variables are initialized only once and carries out the data until the end of the program execution. |
Reference variables | Reference variables contain the references of other variables to perform data manupulations. |
System variables | System variables are predefined variables and accessed by all the programs in SAP environment. |
Structured variables | Structure variable is variable that declared of structure data type. |
Example –
Write a simple program to get clear understanding of local, static variables working.
Code –
*&---------------------------------------------------------------------*
*& Report Z_LOCAL_STATIC
*&---------------------------------------------------------------------*
*& Program coded by TUTORIALSCAMPUS
*&---------------------------------------------------------------------*
REPORT Z_LOCAL_STATIC.
WRITE: 'local variable - static variable'.
* Loop for 5 times.
DO 5 TIMES.
* Perform addition subroutine.
PERFORM addition.
ENDDO.
* Subroutine addition coding.
FORM addition.
* Initializing local variable.
DATA lv_local TYPE i VALUE 99.
* Initializing static variable.
STATICS lv_static TYPE i VALUE 99.
* Adding 1 to local variable.
lv_local = lv_local + 1.
* Adding 1 to static variable.
lv_static = lv_static + 1.
* Display local, static variables.
WRITE: / lv_local, lv_static.
ENDFORM.
Output –
Explaining Example –
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear picture of example code.
Coming to output, lv_local variable is declared in the subroutine and it is local to the addition subroutine. So the lv_local variable got initialized everytime whenever addition subroutine called.
In the above example, we have called addition subroutine 5 times. So, the lv_local variable got initialized with 99 on every time and adds 1 to 99 and display output 100. The same happens for 5 times.
Coming to static variable output, lv_static variable is declared in the subroutine and it is static to the addition subroutine. So the lv_static variable got initialized only when the first time addition subroutine called. During the first sobroutine call, the lv_static variable initialized with 99, adds 1 to 99 and result 100 is displayed for the first time. For the second time subroutine call, the value 100 from the first call retains in lv_static variable, adds 1 to 100 and result 101 displayed in second iteration.
In the above example, we have called addition subroutine 5 times. So, the lv_static variable value incementing by 1 every time and display output 100, 101, 102, 103 and 104.