Statement STATIC Foundation
Declaration and initialization of STATIC variables.
STATIC <VarName> [[:= <Expression>], ... ]
The STATIC declaration declares variables of storage class STATIC. The STATIC variables are initialized only once and their lifetime is the entire runtime. Their visibility is dependent on the declaration:
If STATIC variables are declared within a function or procedure, they are visible only within that function or procedure, and not in any subfunctions or procedures.
If STATIC variables are declared in the source code file before the first FUNCTION or PROCEDURE declaration, their visibility extends to all functions and procedures in that source code file. They are not visible to modules in other files. In order for STATIC variables to have file-wide visibility, you must use the /N compile switch.
STATIC variables keep their value after the function or procedure in which they are declared ends. They are not automatically released. After multiple calls to the function or procedure in which the STATIC variable is declared, the variable contains what was set as of the end of the last call. This also applies to recursive functions. STATIC variables are initialized only once and keep their value.
The STATIC declaration must come before all other executable statements, including the statements PRIVATE, PUBLIC and PARAMETERS. <VarName> can occur only once within a function. If a PRIVATE or PUBLIC variable already exists with the name <VarName>, it is "hidden" and is no longer visible within the function or procedure in which a STATIC variable of the same name is declared.
The length of a variable name is unlimited, but only the first 255 characters are significant. A variable name consists of alphanumeric characters, and the first character may not be numeric. The underscore (_) is permitted as a special character.
A STATIC variable can be initialized in the declaration, using the inline assignment operator (:=). STATIC variables can be initialized with constants or literal arrays of any dimension and size, and the value must be known at compile time. If no assignment is made, STATIC variables have the value NIL. The initialization values are assigned to the variables when the program starts.
The macro operator (&) cannot compile an expression containing the name of a STATIC variable as a character string. The reason is that the references to STATIC variables are resolved during compilation and the symbol name does not exist at runtime.
The data type of STATIC variables can only be determined with the function Valtype() and not with the function Type(). This is because the Type() function uses the macro operator in order to determine the data type of a variable or expression. A character string must always be passed to the function Type(). Valtype() establishes the data type of a value without macros.
In order to be able to track STATIC variables in the debugger, the source code files (PRG files) must be compiled with the /B compiler switch so that the names of the STATIC variables remain available as debug information.
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.