Static variable

Static variable

In computer programming, static variables data value persists for the life of the running process and typically have a broader scope than other variables. Their values may be set at run-time or may change subtly during program execution. The terminology is C and C++ based, but also used in many derived programming languages, in languages with different heritage the same concept may instead be called global variables.

For constants

Computer programs may store constants in constant variables or in static variables, depending on the available features of the programming language. For example, a program that uses an approximation of pi might be easier to write, read, and maintain with a variable called "PI" instead of multiple occurrences of "3.14159"

For scope

In the C programming language, "static" is used with global variables and functions to set their scope to the containing file. In local variables, "static" is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.

For local variables

Most programming languages include the feature of subroutines. Variables local to subroutines (local variables) are usually created and destroyed within the subroutine itself (so-called automatic variables). Some languages, however, (e.g., the C programming language) allow subroutines to "retain" the value of variables between calls, so that the function can preserve its state if necessary. For example, a static variable can record the number of times its subroutine has been executed. Doing so is otherwise possible using global variables or external storage, like a file on disk.

For class variables

Object-oriented programming languages use classes and objects. In this case, a class variable means a variable that is not associated with instances of the class. There is exactly one copy of the variable that is shared among methods of all instances no matter how many or how few instances exist. In C++, class variables are known as static data members.

C# Example

public class Request{ private static int count; private string url;

public Request() { //Create a new instance of Request //Count all requests Request.count++; }

public string Url { get { return this.url; } set { this.url = value; } }

public static int Count { get { //Do not use the this keyword here //as this refers to "THIS INSTANCE"

return Request.count; } //Do not allow the developer to SET this value

C++ Example

class Request{ private: static int count; string url; public: Request() { count++; } string getUrl() const { return url; } void setUrl(string value) { url = value; } static int getCount() { return count; ;int Request::count = 0;

In this sample, count applies to the class while url applies to each instance. Note that the count variable must be initialized outside the class.

References

http://ee.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.6.html

See also

* Variable
* Thread-local storage
* External variable


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • static variable — statinis kintamasis statusas T sritis informatika apibrėžtis Programos kintamasis, kuriam vieta atmintyje paskiriama kompiliavimo metu. Statinio kintamojo reikšmė išlieka visą laiką, kol veikia programa. Todėl baigus veikti paprogramei, kurioje… …   Enciklopedinis kompiuterijos žodynas

  • Variable estática — En informática una variable estática es una variable cuyo valor no varía durante la vida del proceso en ejecución. Normalmente una variable estática tiene un ámbito más amplio que otras variables. Los valores de variables estáticas se pueden… …   Wikipedia Español

  • Variable — A variable (pronEng|ˈvɛərɪəbl) is an attribute of a physical or an abstract system which may change its value while it is under observation. Examples include the height of a child, the temperature across a state, or the input to a function. This… …   Wikipedia

  • Variable data printing — (VDP) (also known as variable information printing (VIP) or VI) is a form of on demand printing in which elements such as text, graphics and images may be changed from one printed piece to the next, without stopping or slowing down the printing… …   Wikipedia

  • Static secondary ion mass spectrometry — Static secondary ion mass spectrometry, or static SIMS is a technique for chemical analysis including elemental composition and chemical structure of the uppermost atomic or molecular layer of a solid which may be a metal, semiconductor or… …   Wikipedia

  • Static memory allocation — refers to the process of allocating memory at compile time before the associated program is executed, unlike dynamic memory allocation or automatic memory allocation where memory is allocated as required at run time. An application of this… …   Wikipedia

  • Static light scattering — is a technique in physical chemistry that measures the intensity of the scattered light to obtain the average molecular weight Mw of a macromolecule like a polymer or a protein. Measurement of the scattering intensity at many angles allows… …   Wikipedia

  • static — [adj] motionless, changeless at a standstill, constant, deadlocked, fixed, format, gridlocked, immobile, immovable, inactive, inert, latent, passive, rigid, stabile, stable, stagnant, stalled, standing still, stationary, sticky, still, stopped,… …   New thesaurus

  • Static single assignment form — In compiler design, static single assignment form (often abbreviated as SSA form or SSA) is an intermediate representation (IR) in which every variable is assigned exactly once. Existing variables in the original IR are split into versions , new… …   Wikipedia

  • Variable (Programmierung) — In der Programmierung ist eine Variable im allgemeinsten Sinne einfach ein Behälter für Rechnungsgrößen („Werte“), die im Verlauf eines Rechenprozesses auftreten. Im Normalfall wird eine Variable durch einen Namen bezeichnet und hat eine… …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”