Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Statics v/s Data

Former Member
2,184

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

1 ACCEPTED SOLUTION

Former Member
375

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.

5 REPLIES 5

former_member181962
Active Contributor
0 Kudos
375

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

alex_m
Active Contributor
0 Kudos
375

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.

Former Member
376

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.

Former Member
0 Kudos
375

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

Former Member
0 Kudos
375

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>