User Tools

Site Tools


static_set_allocate_static_memory_.htm
Navigation:  Language Reference > 5 - Declaration Attributes > Variable and Entity Attributes >====== STATIC (set allocate static memory) ====== Previous pageReturn to chapter overviewNext page

STATIC

blk2blue.jpg

The STATIC attribute specifies permanent memory allocation to a variable, GROUP, or data buffer of a QUEUE. If it is used with variables declared within a PROCEDURE, they are allocated statically instead of using stack memory. STATIC makes values contained in the local variable or QUEUE data buffer “persistent” from one call to the procedure to the next. Static local variables that require initialization are initialized once only during the first call to the respective procedure or routine. Regarding a QUEUE structure, only the data buffer is allocated static memory – QUEUE entries are always allocated memory dynamically on the heap.

NewCNet.jpg

A class method (except CONSTRUCT, DESTRUCT and methods defined in local classes (classes declared locally in a procedure)), field, property, or event can be STATIC. This makes them callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

A static member belongs to the type itself rather than to a specific object.

To access a static member of a class use the following syntax:

class_name.member_name

Example:

AAA        CLASS  

GETINSTANCE PROCEDURE(),AAA,STATIC

          END

! Accessing the static method

FF    &AAA

FF&=AAA.GETINSTANCE()  

SomeProc  PROCEDURE

SaveQueue QUEUE,STATIC          !Static QUEUE data buffer

Field1    LONG                  !Value retained between

Field2    STRING                !procedure calls

         END

AcctFile  STRING(64),STATIC     !STATIC needed for use as

                               !variable in NAME attribute

Transactions FILE,DRIVER('Clarion'),PRE(TRA),NAME(AcctFile)

AccountKey    KEY(TRA:Account),OPT,DUP

Record         RECORD

Account         SHORT           !Account code

Date            LONG            !Transaction Date

Amount          DECIMAL(13,2)   !Transaction Amount

              END

            END

See Also:

Data Declarations and Memory Allocation

static_set_allocate_static_memory_.htm.txt · Last modified: 2021/04/15 15:57 (external edit)