HOME

TheInfoList



OR:

In some
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s such as C (and its close descendants like
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
,
Objective-C Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTS ...
, and
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
), static is a
reserved word In a computer language, a reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use". This is a syntactic definition, and a re ...
controlling both lifetime (as a
static variable In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program. This is in contrast to shorter-lived automatic variables, whose storage is ...
) and visibility (depending on ''
linkage Linkage may refer to: * ''Linkage'' (album), by J-pop singer Mami Kawada, released in 2010 *Linkage (graph theory), the maximum min-degree of any of its subgraphs *Linkage (horse), an American Thoroughbred racehorse * Linkage (hierarchical cluster ...
''). The effect of the keyword varies depending on the details of the specific programming language.


Common C/C++ behavior

In C and C++, the effect of the static keyword in C depends on where the declaration occurs. static may act as a
storage class Storage may refer to: Goods Containers * Dry cask storage, for storing high-level radioactive waste * Food storage * Intermodal container, cargo shipping * Storage tank Facilities * Garage (residential), a storage space normally used to store ...
(not to be confused with classes in object-oriented programming), as can
extern In the C programming language, an external variable is a variable defined outside any function block. On the other hand, a local (automatic) variable is a variable defined inside a function block. Definition, declaration and the extern keywor ...
,
auto Auto may refer to: * An automaton * An automobile * An autonomous car * An automatic transmission * An auto rickshaw * Short for automatic * Auto (art), a form of Portuguese dramatic play * ''Auto'' (film), 2007 Tamil comedy film * Auto (play), ...
and
register Register or registration may refer to: Arts entertainment, and media Music * Register (music), the relative "height" or range of a note, melody, part, instrument, etc. * ''Register'', a 2017 album by Travis Miller * Registration (organ), th ...
(which are also reserved words). Every variable and function has one of these storage classes; if a declaration does not specify the storage class, a context-dependent default is used: *extern for all top-level declarations in a source file, *auto for variables declared in function bodies. In these languages, the term "static variable" has two meanings which are easy to confuse: # A variable with the same lifetime as the program, as described above (language-independent); ''or'' # (C-family-specific) A variable declared with storage class static. Variables with storage class extern, which include variables declared at top level without an explicit storage class, are static in the first meaning but not the second.


Static global variable

A variable declared as static at the top level of a source file (outside any function definitions) is only visible throughout that file ("
file scope In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts ...
", also known as "
internal linkage Internal may refer to: *Internality as a concept in behavioural economics *Neijia, internal styles of Chinese martial arts *Neigong or "internal skills", a type of exercise in meditation associated with Daoism *''Internal (album)'' by Safia, 2016 ...
"). In this usage, the keyword static is known as an " access specifier".


Static function

Similarly, a static functiona function declared as static at the top level of a source file (outside any class definitions)is only visible throughout that file ("
file scope In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts ...
", also known as "
internal linkage Internal may refer to: *Internality as a concept in behavioural economics *Neijia, internal styles of Chinese martial arts *Neigong or "internal skills", a type of exercise in meditation associated with Daoism *''Internal (album)'' by Safia, 2016 ...
").


Static local variables

Variables declared as static inside a function are statically allocated, thus keep their memory location throughout all program execution, while having the same scope of visibility as automatic local variables (auto and register), meaning they remain local to the function. Hence whatever values the function puts into its
static local variable In computer science, a local variable is a variable that is given ''local scope''. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope. In programming languages with on ...
s during one call will still be present when the function is called again.


C++ specific


Static member variables

In
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, member variables declared as static inside class definitions are
class variable In class-based, object-oriented programming, a class variable is a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist. A class variable is not an instance variable. It is a special ...
s (shared between all class instances, as opposed to
instance variable In class-based, object-oriented programming, an instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated object of the class has a separate copy, or instance. An instance variable has similariti ...
s).


Static method

Similarly, a
static method A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be utilized by any of ...
a method declared as static inside a class definitionis meant to be relevant to all instances of a class rather than any specific instance. A method declared as static can be called without instantiating the class.


Java

This keyword static means that this method is now a class method; it will be called through class name rather than through an object. A static method is normally called as .methodname(), whereas an instance method is normally called as .methodname().


See also

{{Portal, Computer programming C (programming language) category: C++ category: Java (programming language)