ArticleslgStudy

science

Static (keyword)

Static (keyword) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Static (keyword) rather than just read about it. In short: 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.

Key takeaways

  • Static (keyword) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Static (keyword) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Static (keyword) from memory before moving on to harder problems.

Reference excerpt

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.

Origin In the predecessors of C, including BCPL and B, there was already a concept of static storage, 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 the auto keyword, which declared a variable with automatic storage – one appears whenever the function is invoked, and disappears when the function returns. 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 keywords were added as storage class specifiers, along with auto and extern, which kept their meaning from B. However, in C, the concept of linkage for variables outside of 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:

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 and auto for variables declared in function bodies. So, in C, although the static keyword – when used on variables – always declares a variable with static storage duration, there are two distinct meanings of the keyword, depending on where it is used:

Top-level declaration: to declare an identifier with internal instead of external linkage, applicable to both variables and functions. Block-scoped declaration: to declare a variable with static instead of automatic storage duration. C doesn't support block-scoped functions. Therefore, in C, 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. 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 (i.e. a 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. Programming languages which support the usage of static keyword for local variables to be kept across invocations include C, C++, Objective-C, C#, PHP and PL/I. 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 use 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#.

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:

Independent from any class instances, can be accessed using the class name instead of an expression having the class object type. For fields: exist throughout the program lifetime (i.e. having static storage duration). For methods: cannot access any non-static members without an object expression (implicitly referring to the current instance), or the this reference for the current instance. Some programming languages even go further, allowing the use of static keyword in other places in a class declaration or usage, for example:

Static initialisers in Java and JavaScript, which are run once when the class is loaded Static constructors in C#, which are run once before the class is used Static classes in C#, which can't be instantiated. Inner classes in Java implicitly carry a this reference to the outer class object and must exist in the context of an outer class instance. By declaring an inner class to be static, it does not carry such a reference and can be used independently to an outer class instance. The static keyword can be used in place of an actual class name to access static members to provide class-level polymorphism, called late static bindings

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.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Static (keyword)

Start with the simplest possible case. Write down what Static (keyword) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Static (keyword) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Static (keyword) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Static (keyword)

In research
Static (keyword) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Static (keyword) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Static (keyword) is common in secondary-school and first-year university syllabi. It links to neighbouring topics C++, C (programming language), Java (programming language), so understanding it makes those chapters shorter.
In everyday life
Look for Static (keyword) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Static (keyword)” →

How to study Static (keyword) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Static (keyword) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Static (keyword) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Static (keyword) in simple terms?

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 cl…

Why does Static (keyword) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Static (keyword)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Static (keyword).

Tags

  • C++
  • C (programming language)
  • Java (programming language)

Keep exploring