xHarbour Reference Documentation > Statement Reference |
Declares and optionally initializes a STATIC memory variable.
STATIC <varName> [:= <xValue> ]
The STATIC statement declares a memory variable that has STATIC scope. Static variables are resolved by the compiler, i.e. their symbolic name cannot be retrieved during runtime. This makes acces to STATIC variables much faster than to memory variables of PRIVATE or PUBLIC scope, whose symbolic variable names exist at runtime.
The names of STATIC variables cannot be included in macro-expressions since they cannot be resolved by the macro operator (&). This operator requires the symbolic name of a variable to exist at runtime.
The lifetime of STATIC variables is identical with the lifetime of a program. Unlike LOCAL variables, whose values are discarded when the declaring routine executes the RETURN statement, STATIC variables keep their values during the entire execution cycle of a program. The value assigned to a STATIC variable is stored in this variable until it gets assigned a new value.
The visibility of STATIC variables depends on the place of declaration:
1. | When the STATIC declaration appears at the top of a PRG file before any other executable statement, the STATIC variable has file wide scope, i.e. it can be seen by all routines programmed in that PRG file. |
2. | When the STATIC declaration follows a FUNCTION, METHOD or PROCEDURE declaration, the variable can be seen only in the routine that declares the STATIC variable. |
3. | STATIC variables cannot be seen outside the PRG file that contains the STATIC declaration. |
When a routine executes the RETURN statement, all STATIC variables declared in that routine keep their values.
The lines in PRG source code preceding the STATIC statement may not call executable code. They can only contain declaration statements, i.e. only the FUNCTION, METHOD, PROCEDURE statements, and the FIELD, LOCAL, MEMVAR or PARAMETERS variable declarations are allowed to precede the STATIC statement.
It is possible to initialize a static variable already in the STATIC statement. To accomplish this, the inline-assignment operator must be used. The values assigned to STATIC variables must be programmed as literal values, i.e. the compiler must be able to resolve values assigned to STATIC variables. It is not possible, for example, to initialize a STATIC variable with the RETURN value of a function, since this can only be resolved at runtime of a program.
Note: symbolic name as a STATIC variable, only the STATIC variable is visible. PRIVATE or PUBLIC variables with the same name become visible again, when the STATIC variable gets out of scope, i.e. when the routine that declares the STATIC variable returns or calls a subroutine.
See also: | FUNCTION, GLOBAL, LOCAL, PARAMETERS, PRIVATE, PROCEDURE, PUBLIC |
Category: | Declaration , Statements |
// The example uses one PRIVATE variable and two STATIC variables to // demonstrate lifetime and visibility of STATIC variables. STATIC snFileWide := 100 PROCEDURE Main PRIVATE myVar := 10 ? ProcName(), snFileWide, myVar // output: MAIN 100 10 Test1() // output: TEST1 100 3 Test2() // output: TEST2 110 12 ? snFileWide // output: 120 ? myVar // output: 12 RETURN PROCEDURE Test1() STATIC myVar := 1 ? ProcName(), snFileWide, myVar += 2 // output: TEST1 100 3 snFileWide += 10 RETURN PROCEDURE Test2() ? ProcName(), snFileWide, myVar += 2 // output: TEST2 110 12 snFileWide += 10 RETURN
http://www.xHarbour.com