4

I've heard there are differences between languages about the meaning of the keyword static, but I've not found a good list that consolidates those differences.

Here's what I know about the meaning of static in C++:

  • For local static variables within a function, the variable is initialized at startup and the value is saved across function calls.
  • Static data members are shared among all instances of a class. In other words, there is only one instance of a static data member. Static data members must be initialized at file scope.
  • Static member functions have access only to static members.
  • In recursive code, a static object or variable is guaranteed to have the same state in different instances of a block of code.
  • Static objects and variables defined at file scope only have internal linkage. No other files may use them.

How does the meaning of static change in other languages?

Scottie T
  • 11,729
  • 10
  • 45
  • 59
  • Static locals in C++ are initialized on first invocation, not on startup. –  Mar 06 '09 at 19:16
  • @Iraimbilanja, can you give me a reference for that? MSDN seems to suggest otherwise. – Scottie T Mar 06 '09 at 19:41
  • @Iraimbilanja: C&V 6.7 (see my post below for the exact line). – dirkgently Mar 06 '09 at 19:48
  • scottie: well... struct A { A() { cout << "f"; } }; void f() { static A a; } int main() { cout<<"m"; f(); } //prints mf –  Mar 07 '09 at 14:45
  • Sure, that just means the output is executed in order. That doesn't prove the struct isn't initialized at start-up. Honestly, I'm not invested in either answer, I'd just liked to know the right one. – Scottie T Mar 07 '09 at 21:32
  • Define "initialized" then. Unless you mean "having its bytes filled with zeros", it's pretty obvious that the object here is initialized on first invocation. –  Mar 09 '09 at 05:54
  • That's exactly what I mean, memory is made available and its value is set to zero. – Scottie T Mar 09 '09 at 16:32
  • Right. In that case it's done on startup. However, you should know that in C++, we call an object "initialized" only when its constructor has ran - i.e., when it is in a valid state for use. –  Mar 09 '09 at 19:11

9 Answers9

11

C


  • The keyword can change either the linkage or the duration (lifetime) of an object.
  • Variables are always initialized to 0
  • Functions have internal linkage.
  • If declared in file level scope: variables have internal linkage and static duration (i.e. exists throughout the lifetime of the program)
  • If declared in block scope: variables have no linkage but static duration
  • There can multiple declarations of the same static variable in a translation unit. However, note that they must be the same. E.g: at file-level scope:

int a;        // a has external linkage

static int a; // a now has static linkage
              // same as if you wrote: static int a = 0;

//...

static int b; // static linkage

extern int b; // extern loses its meaning, b still has internal linkage

//...

extern int b; // b has external linkage

static int b; // error

//...

void func() {
  static int x; // automatic linkage, static duration
                // same as if you wrote: static int x = 0;
}

C++


  • At file level scope the usage has been deprecated for both variables and members in favor of anonymous namespaces. Exists only as compatibility
  • Variables still get default initialized (as in C) to 0
  • "6.7 The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place [...] "
  • Variables have static storage duration unless accompanied by a thread_local specifier (from C++0x onwards)
  • There can be only one definition of a static in a translation unit
  • Member variables/functions mean they are properties of the class and not the instances Legal access syntax: instance.property or Class::property
  • Static member functions can only access only static member variables No this pointer for such functions
  • Non-static members can however access any static member
  • At file level objects have internal linkage except for class members which have a class scope
  • Class members need to be defined either in the class declaration or outside explicitly via class name and scope resolution operator
  • Cannot use this in a static method

ActionScript


  • Class methods as in C++
  • cannot use this or super in a static method
  • Accessed only through class name and not instance name
  • Not inherited
  • Derived classes however have access to bases' static properties
  • Variables that are declared with both the static and const keywords must be initialized at the same time as you declare the constant

Object Oriented Design


  • The Singleton design pattern is considered by many as a glorified static object
  • Used in Factory design pattern

I may have missed a lot of other things -- feel free to chip in.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
5

In Delphi the static keyword is used exclusively for defining class methods. In Delphi a normal class method can be declared virtual and overridden in a subclass. Additionally Delphi has a self variable, similar to the this pointer in other languages. However in a class method the self points to the class in which the method is called instead of an instance.

Declaring a class method static means:

  1. It cannot be overridden in a subclass
  2. It does not have a self pointer

This means a static class method can only access class members in the class it was defined in, while a normal class method can access overridden class members in derived classes.

There are other informal uses of static in the Delphi documentation usually referring to a feature unchangability (is that a word?). For instance a static array vs a dynamic array. All instance methods in Delphi are static unless declared otherwise.

Kromster
  • 7,181
  • 7
  • 63
  • 111
Kenneth Cochran
  • 11,954
  • 3
  • 52
  • 117
2

In C# it pretty much always means: "related to a type rather than an instance of the type".

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @R Bemrose - well nearly. "static import" doesn't feel quite the same; it's certainly not the "type equivalent" of a non-static import, for example. – Jon Skeet Mar 06 '09 at 19:34
  • But extension methods, which are always "related to an instance of a type", can only be declared in static classes... breaking the nice rule-of-thumb. – bzlm Apr 13 '09 at 15:19
  • Well, sort of - and sort of not. The instance is provided as an argument in that case. The point is that there's no *inherent* context. – Jon Skeet Apr 13 '09 at 16:49
2

In VB.NET, a Static variable is just like a C++ local static variable.

However, there is no class-wide Static; use Shared instead.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
2

In C# there are 3 ways a static keyword can be used:

  • On class definition, which means the class will only expose static members and cannot be instanciated
  • On a class member, which means that the member can be called without having to instanciate the class.
  • On a constructor, which means that the static constructor will allways be called before a static member is called. (Those are mostly performance bottlenecks and therefore not recommended)

Hope this helps.

Jeroen Landheer
  • 9,160
  • 2
  • 36
  • 43
  • Don't forget that on class definition it also means the class can declare extension methods. Also, "can be called without having to instanciate the class" is a very unorthodox way of describing it. And on a constructor, isn't the important thing that it becomes a "class constructor" instead? – bzlm Apr 13 '09 at 15:21
  • 1
    If you have a class with 3 static members and one static contructor, the static contructor is always called before execution of the static member, every time a member is called. This is different from a class contructor, because a class contructor is called only when the class is instanciated. – Jeroen Landheer Apr 16 '09 at 19:43
1

Python has the decorator @staticmethod, which when applied to a class member, makes the method available on the class rather than instances, and passes no automatic arguments to the method. The @classmethod decorator performs a similar function, but passes the class as the first argument, making it much more useful.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
1

In C, static flags a function or global variable as local to the file its located in.

It's kinda like private in other languages. Sorta.

If it's in a function, then static preallocates that variable in the data section of the binary, rather than on the stack at run time.

Patrick
  • 90,362
  • 11
  • 51
  • 61
1

In VB.NET static is used procedure level to mean the variable is associated with all executions of the procedure (it's preserved from call to call). That's a bit of an arcane usage in an Object-Oriented application though.

The counterpart is "Shared" which means the method or member is type level (you don't need an instance to access it).

Eric Nicholson
  • 4,053
  • 1
  • 28
  • 29
0

Wikipedia summarizes many different meanings of static:

Static Methods, static variables, static typing.

Ben S
  • 68,394
  • 30
  • 171
  • 212