2007 Apr 09 1:38 PM
Hi,
Can any one here describe me the exact difference between <b>statics</b> and <b>data</b> statements?
I got two words local visibility and static validity in the documentation regarding statics, but really don't know what it is meant for.
Thanks in advance,
Ravi
2007 Apr 09 1:51 PM
Hi,
statics will be initialized only once in a program where as data will be initialized again and again.
check this simple code.
DO 10 TIMES.
PERFORM add_one.
ENDDO.
FORM add_one.
DATA local TYPE i VALUE 10.
STATICS static TYPE i VALUE 10.
local = local + 1.
static = static + 1.
WRITE: / local, static.
ENDFORM.
regards,
bharat.
2007 Apr 09 1:45 PM
See the following example given in the documentation:
DO 10 TIMES.
PERFORM add_one.
ENDDO.
FORM add_one.
DATA local TYPE i VALUE 10.
STATICS static TYPE i VALUE 10.
local = local + 1.
static = static + 1.
WRITE: / local, static.
ENDFORM.
Output would be:
11 11
11 12
11 13
11 14
11 15
11 16
11 17
11 18
11 19
11 20
Regards,
Ravi
Message was edited by:
Ravi Kanth Talagana
2007 Apr 09 1:47 PM
Variables that you declare with the DATA statement live for as long as the context in which they are defined. So variables in an ABAP main program exist for the entire runtime of the program, and local variables in procedures only exist for as long as the procedure is running.
To retain the value of a local variable beyond the runtime of the procedure, you can declare it using the STATICS statement. This declares a variable with the lifetime of the context of the main program, but which is only visible within the procedure.
The first time you call a subroutine or function module, the corresponding main program is always loaded into the internal session of the calling program. It is not deleted when the procedure ends. This enables variables defined using STATICS to retain their values beyond the runtime of the procedure, allowing them to be reused the next time the procedure is called.
2007 Apr 09 1:51 PM
Hi,
statics will be initialized only once in a program where as data will be initialized again and again.
check this simple code.
DO 10 TIMES.
PERFORM add_one.
ENDDO.
FORM add_one.
DATA local TYPE i VALUE 10.
STATICS static TYPE i VALUE 10.
local = local + 1.
static = static + 1.
WRITE: / local, static.
ENDFORM.
regards,
bharat.
2007 Apr 09 1:55 PM
Hi..,
STATICS and DATA both are used to declare variables..
STATICS is generally used in Form .... Endform, we can use outside also but no use...
DATA can be used anywhere to define the variables..
observe this code..
DO 5 TIMES.
PERFORM local_static.
ENDDO.
FORM local_static.
DATA local TYPE i .
STATICS lstat TYPE i .
local = local + 1.
lstat = lstat + 1.
WRITE: / local, lstat.
ENDFORM.
**************************
output is..
1 1
1 2
1 3
1 4
1 5
***************************
STATICS stores the value even after the Perform is closed..
so its lifetime is till the end of the program..
but the lifetime of DATA defined variable is till the end of the FORM..
Hope u understood !!!!
regards,
sai ramesh
2007 Apr 09 2:06 PM
hi
plz check this out.
<b>A local variable is a variable that is defined inside a subroutine using the local, data, or statics statement. It is said to be local to the subroutine. Variables defined by using local are accessible from outside the subroutine; variables defined by using data or statics are not. Thus if the subroutine calls another subroutine, variables defined by using local are visible from within the called subroutine-variables defined by using data or statics are not.
For local variables defined by using local or data, memory is allocated each time the subroutine is called. That memory is freed when the subroutine ends, and so the values within it are lost. For statics, the memory is retained.</b>
hope it will be helpful for u.
regards
ravish
<b>plz reward if helpful</b>