Static variable

Wikipedia, the free encyclopedia - Cite This Source

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.

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 in 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 can be seen and modified by all methods (called member functions in C++) of the class. This means, for example, that if the value of a class variable is changed by any method of the owning class, then all other methods of that class will see the changed value. 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 Url(string value) { url = value; return url; } int Count() const { 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

See also



Wikipedia, the free encyclopedia © 2001-2006 Wikipedia contributors (Disclaimer)
This article is licensed under the GNU Free Documentation License.
Last updated on Wednesday March 12, 2008 at 14:06:53 PDT (GMT -0700)
View this article at Wikipedia.org - Edit this article at Wikipedia.org - Donate to the Wikimedia Foundation