Static (keyword)

Last updated

static is a reserved word in many programming languages to modify a declaration. The effect of the keyword varies depending on the details of the specific programming language, most commonly used to modify the lifetime (as a static variable) and visibility (depending on linkage ), or to specify a class member instead of an instance member in classes.

Contents

Origin

In the predecessors of C, including BCPL and B, there was already a concept of static storage. [1] [2] , which meant a storage which is always in existence. However, In B, there wasn't a static keyword, but there was an extrn keyword to specify external storage (external to all functions and must be defined outside a function), which is always static, in contrast with auto keyword, which declared an variable with auto storage - one appears whenever the function is invoked, and disappears when the function is left. [2] . All variables must be declared, as one of auto, extrn, or implicitly as function arguments.

C was developed as a successor of B, and the static and register keyword were added as storage class specifiers, alongside with auto and extern , which kept their meaning from B. However, in C, the concept of linkage for variables outside functions was introduced. A C program can be formed by multiple compilation units and linked together to form a complete program, which a variable or a function can be either specified as having internal linkage (visible to its own compilation unit only), or external linkage (visible to the whole program). These keywords specify both the storage duration and linkage as follows [3] :

Storage classDurationLinkage
extern static (program execution)external (whole program)
static static (program execution)internal (translation unit only) for top-level identifier
none for block-scope variables
auto, register function execution none

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:

So, in C, although the static keyword, when used on variables, always declare a variable with static storage duration, there are two distinct meanings of the keyword, depending on where it is used:

Therefore, in C, the term "static variable" has two meanings which are easy to confuse:

  1. A variable with the same lifetime as the program, as described above (language-independent); or
  2. (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.

In C++ (not C), the static keyword can also be used to declare a member variable in a class to have static storage duration as well, independent from the storage duration of the class object itself, and such a variable must be defined outside the class. The effect is that the variable is shared among all class instances, becoming a class member instead of an instance member. When applied to a member function (method), it specifies that the member function operates independently of an instance, which means it can't access any non-static members of the class nor the this pointer.

As a storage duration specifier

The static keyword is used in many programming languages to specify a local variable to have a lifetime of the whole program, preserved between function invocations, instead of having its own copy for each function invocation as in automatic storage duration, inherited from the usage in C.

Examples of programming languages which support the usage of static keyword for local variables to be kept across invocation include such as C, C++, Objective-C, C#, PHP.

The following programming languages with C-like syntax do not support static local variables as in C: Java, JavaScript, Dart. In these languages, a variable which is kept for the whole program execution needs to be declared outside functions.

As a visibility specifier

The static keyword in C, when used as a top-level declaration, makes the variable or function visible to its own compilation unit only. Modern programming languages generally uses namespaces to avoid name clashes, so this use isn't widely adopted apart from programming languages with C compatibility in mind (e.g. C++, Objective-C). Other programming languages which support visibility declarations at top-level use a variety of other keywords to specify different level of visibility, for example, public in Java for classes which can be used everywhere, or internal / file in C#. [4]

As a class member specifier

The static keyword is used in most programming languages with classes to specify a member to be a class member, instead of an instance member, inherited from the usage of C++.

A static member of a class has the following characteristics:

Some programming languages even go further, allowing the use of static keyword in other places in a class declaration or usage, for example:

As a specifier for closures

The static keyword can be used in some programming languages on anonymous functions to prevent capturing states which are captured by default. It is similar to static local variables and static class members in the sense that a static closure does not depend on the running invocation of the containing function.

See also

  1. "MUL TIC SYSTEf·-1-PROGRAMMERS' MANUAL" (PDF).
  2. 1 2 Kernighan, B. W. A TUTORIAL INTRODUCTION TO THE LANGUAGE B (PDF). Murray Hill, New Jersey: Bell Laboratories. p. 4.
  3. "Storage-class specifiers". cppreference.com.
  4. "Access Modifiers (C# Programming Guide)". Microsoft.
  5. "Late Static Bindings". PHP Manual.
  6. "Static anonymous functions". Microsoft.

Related Research Articles

In computer programming, the scope of a name binding 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 of the program, the name may refer to a different entity, or to nothing at all. Scope helps prevent name collisions by allowing the same name to refer to different objects – as long as the names have separate scopes. The scope of a name binding is also known as the visibility of an entity, particularly in older or more technical literature—this is in relation to the referenced entity, not the referencing name.

In computer programming, a global variable is a variable with global scope, meaning that it is visible throughout the program, unless shadowed. The set of all global variables is known as the global environment or global state. In compiled languages, global variables are generally static variables, whose extent (lifetime) is the entire runtime of the program, though in interpreted languages, global variables are generally dynamically allocated when declared, since they are not known ahead of time.

In the C and C++ programming languages, an inline function is one qualified with the keyword inline; this serves two purposes:

  1. It serves as a compiler directive that suggests that the compiler substitute the body of the function inline by performing inline expansion, i.e. by inserting the function code at the address of each function call, thereby saving the overhead of a function call. In this respect it is analogous to the register storage class specifier, which similarly provides an optimization hint.
  2. The second purpose of inline is to change linkage behavior; the details of this are complicated. This is necessary due to the C/C++ separate compilation + linkage model, specifically because the definition (body) of the function must be duplicated in all translation units where it is used, to allow inlining during compiling, which, if the function has external linkage, causes a collision during linking. C and C++ resolve this in different ways.
<span class="mw-page-title-main">C syntax</span> Set of rules defining correctly structured programs

The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

This article compares two programming languages: C# with Java. While the focus of this article is mainly the languages and their features, such a comparison will necessarily also consider some features of platforms and libraries. For a more detailed comparison of the platforms, see Comparison of the Java and .NET platforms.

In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime is the entire run of the program. This is in contrast to shorter-lived automatic variables, whose storage is stack allocated and deallocated on the call stack; and in contrast to dynamically allocated objects, whose storage is allocated and deallocated in heap memory.

In some programming languages, const is a type qualifier that indicates that the data is read-only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in that it is part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In other languages, the data is not in a single memory location, but copied at compile time for each use. Languages which use it include C, C++, D, JavaScript, Julia, and Rust.

In computer programming, thread-local storage (TLS) is a memory management method that uses static or global memory local to a thread. The concept allows storage of data that appears to be global in a system with separate threads.

In computer programming, an automatic variable is a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope. The scope is the lexical context, particularly the function or block in which a variable is defined. Local data is typically invisible outside the function or lexical context where it is defined. Local data is also invisible and inaccessible to a called function, but is not deallocated, coming back in scope as the execution thread returns to the caller.

In computer programming, a declaration is a language construct specifying identifier properties: it declares a word's (identifier's) meaning. Declarations are most commonly used for functions, variables, constants, and classes, but can also be used for other entities such as enumerations and type definitions. Beyond the name and the kind of entity, declarations typically specify the data type, or the type signature ; types may also include dimensions, such as for arrays. A declaration is used to announce the existence of the entity to the compiler; this is important in those strongly typed languages that require functions, variables, and constants, and their types to be specified with a declaration before use, and is used in forward declaration. The term "declaration" is frequently contrasted with the term "definition", but meaning and usage varies significantly between languages; see below.

The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages are often integrated into a single product, with the programmer able to specify C or C++ as their source language.

C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior version of the C++ standard, named C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

In the C programming language, and its predecessor B, 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.

As an alternative to automatic variables, it is possible to define variables that are external to all functions, that is, variables that can be accessed by name by any function. Because external variables are globally accessible, they can be used instead of argument lists to communicate data between functions. Furthermore, because external variables remain in existence permanently, rather than appearing and disappearing as functions are called and exited, they retain their values even after the functions that set them have returned.

In programming languages, particularly the compiled ones like C, C++, and D, linkage describes how names can or can not refer to the same entity throughout the whole program or one single translation unit.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python, and others manipulate data structures.

In computer programming, a constant is a value that is not altered by the program during normal execution. When associated with an identifier, a constant is said to be "named," although the terms "constant" and "named constant" are often used interchangeably. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution. To simplify, constants' values remains, while the values of variables varies, hence both their names.