Static variable

Static is an additive in the declaration of variables in programming languages ​​such as C, C , Java or C #, VB.NET.

As static storage class

The static storage class defines the "lifetime" and the binding of a variable - it will exist or carry the last assigned value during the entire term of a translation unit; see foreign translation units with direct access principle only independent copies. If, for example, the actual current value of the static variable A from translation unit X is read, it must be used from unit X necessarily a function / method / property or interface; without this is always an A (possibly statically initialized ) copy for unit Y.

Static as Typqualifikator

In C and C is specified by this Typqualifikator that the associated variable exists physically within this class, and instance - independent only once. Such data are relevant, for example in shared access to unique data fields, especially within multi-threaded processes, or when using multiple processes - in such a scenario, a mutex could be declared with static and thus enable sequencing of the data accesses of different threads.

Even as a unique data field may be useful static; For example, it may be useful under certain circumstances, an Ethernet data connection ( see also socket ) to declare such. Access to this connection can take place over a surrounding class or a module and include additional functionality for flow control, for example - a real example would be a TCP port for an RS232 port because the port does not allow multiple connections, would be a Mehrfachkonnektierung various feasible LAN or WAN clients only with additional effort.

Example in VB.NET

In VB.NET, a declaration as static in the global context, or even outside of a function / procedure in contrast to other languages ​​is not possible for this keyword exists shared.

The following example shows a trivial application within threads; a static variable is initialized only once, even if the current procedure / the thread current iteration has been completed and the logic according to another thread should now re-initialize:

Imports System.Threading Public Class Static Demo     Private Shared ReadOnly mMutex As New Mutex '1 copy for all threads     Public Sub StartThreads ()        For i = 1 To 10 As UInteger           ThreadPool.QueueUserWorkItem (New WaitCallback ( AddressOf IncrementThread ) ) start '10 threads        Next     end Sub       Private Sub IncrementThread ()        Dim tId As Integer = Thread.CurrentThread.ManagedThreadId ' here everyone can access be tracked        Static MNUMBER As UInteger = 0 '1 copy for all threads, context Limited        while mMutex.WaitOne           MNUMBER = 1           mMutex.ReleaseMutex ()        end While        end Sub end Class Example in C

The following example shows the functionality of this keyword in C:

# include   void func () {          static int x = 0; / / X is initialized only once, in spite of multiple calls in main ()          printf (" % d \ n", x); / / Returns the value of x          x = x 1; }   int main (int argc, char * const argv [ ]) {          func ( ); / / Output: 0          func ( ); / / Output: 1          func ( ); / / Output: 2          return 0; } Example in C

Class Request {          private:                  static int count = 0; / / Counter for all calls                  string url;            public:                  Request ( ) { count ; }                  string getUrl () const {return url; }                  void setUrl (string value) { url = value; }                  static int getCount () {return count; } }; References

726107
de