Static (keyword)

This article is missing information about the use of the static keyword to declare class methods in C++ and Java. Please expand the article to include this information. Further details may exist on the talk page. (April 2014)

|

In some programming languages such as C (and its close descendants like C++, Objective-C, and Java), `static` is a reserved word controlling both lifetime (as a static variable) and visibility (depending on linkage). The effect of the keyword varies depending on the details of the specific programming language.

## Contents

* 1 Common C/C++ behavior
* 1.1 Static global variable
* 1.2 Static function
* 1.3 Static local variables
* 2 C++ specific
* 2.1 Static member variables
* 2.2 Static method
* 3 Java

## 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 (not to be confused with classes in object-oriented programming), as can `extern`, `auto` and `register` (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.

Storage class  | Lifetime  | Visibility

|
|

`extern` | program execution  | external (whole program)
`static` | program execution  | internal (translation unit only)
`auto`, `register` | function execution | (none)

In these languages, 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.

### 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", also known as "internal linkage"). In this usage, the keyword `static` is known as an "access specifier".

### Static function

Similarly, a static function - a function declared as `static` at the top level of a source file (outside any class definitions) - is only visible throughout that file ("file scope", also known as "internal linkage").

### 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 variables during one call will still be present when the function is called again.

## C++ specific

### Static member variables

In C++, member variables declared as `static` inside class definitions are class variables (shared between all class instances, as opposed to instance variables).

### Static method

Similarly, a static method - a method declared as `static` inside a class definition - is 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

category: C++ category: Java (programming language)

This article does not cite any external source. HandWiki requires at least one external source. See citing external sources. Please help improve this article. Unsourced material may be removed in future.
Find sources: "Static" keyword – news · newspapers · books · scholar · JSTOR (2021) (Learn how and when to remove this template message)

|

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Static (keyword). Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Static_(keyword)&oldid=286697"