Static in different languages

I heard that there are differences between languages ​​about the meaning of the static , but I have not found a good list that combines these differences.

Here is what I know about the static value in C ++:

  • For local static variables inside a function, the variable is initialized at startup, and the value is stored in all function calls.
  • Elements of static data are distributed among all instances of the class. In other words, there is only one instance of a static data element. Static data elements must be initialized in the file area.
  • 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 the code block.
  • Static objects and variables defined in the file area have an internal relationship. No other files can use them.

How does the static value change in other languages?

+3
programming-languages static keyword
Mar 06 '09 at 19:02
source share
9 answers

FROM




  • A keyword can change either the binding or the duration (lifetime) of an object.
  • Variables are always initialized 0
  • Functions have an internal connection.
  • If declared in the file level area: variables have an internal relationship and a static duration (i.e., exist throughout the entire program life cycle)
  • If declared in block scope: variables have no binding, but static duration
  • A translation block may have several declarations of the same static variable. However, keep in mind that they must be the same. For example: in the file level area:



 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 ++




  • In the file level domain, usage is deprecated for both variables and members in favor of anonymous namespaces. There is only compatibility.
  • Variables still get default initialization (as in C) to 0
  • "6.7 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 [...]"
  • Variables have a static storage duration if it is not accompanied by the thread_local (starting with C ++ 0x)
  • There can only be one definition of static per translation unit.
  • Variable members / functions mean that they are class properties, not instances. Legal access syntax: instance.property or Class :: property
  • Static member functions can only access static member variables. This pointer for such functions
  • Non-static members can, however, refer to any static member
  • File-level objects have an internal relationship, with the exception of class members that have the scope class
  • Class members must be defined either in the class declaration or externally explicitly through the class name and scope resolution operator.
  • Cannot use this in static method



ActionScript




  • Class methods like in C ++
  • can't use this or super in static method
  • Access only through class name, not instance name
  • Not inherited
  • Derived classes, however, have access to static database properties.
  • Variables declared by both static and constant keywords must be initialized simultaneously with the declaration of a constant



Object oriented design




  • The Singleton design pattern is seen by many as an illustrious static object.
  • Used in Factory Design Pattern



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

+8
Mar 06 '09 at 19:36
source share

In Delphi, the static keyword is used exclusively to define class methods. In Delphi, the Regular Class Method can be declared virtual and overridden in a subclass. In addition, Delphi has a self variable similar to this pointer in other languages. However, in the class method itself points to the class in which the method is called instead of the instance.

Declaring a static class method means:

  • It cannot be overridden in a subclass.
  • It does not have a pointer to self

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

There is another unofficial use of statics in the Delphi documentation, usually referring to function insensitivity (is that a word?). For example, a static array versus a dynamic array. All instance methods in Delphi are static unless otherwise specified.

+4
Mar 06 '09 at 19:44
source share

In VB.NET, the Static variable is similar to a C ++ local static variable.

However, there is no Static for the class; Use Shared instead.

+2
Mar 06 '09 at 19:17
source share

In C #, this almost always means: "associated with a type, not an instance of a type."

+1
Mar 06 '09 at 19:05
source share

Python has an @staticmethod decorator, which when applied to a member of a class makes this method available to the class, not instances, and does not pass 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.

+1
Mar 06 '09 at 19:12
source share

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

It looks like a private language in other languages. Varieties.

If it is in a function, then static predefines this variable in the data section of the binary file, and not on the stack at run time.

+1
Mar 06 '09 at 19:21
source share

VB.NET static uses the procedure level to mean that a variable is associated with all executions of a procedure (it is preserved from call to call). This is a bit of a secret use in an object-oriented application.

Another is β€œShared,” which means that a method or member is a type level (you don't need an instance to access it).

+1
Mar 06 '09 at 19:30
source share

In C #, there are 3 ways to use a static keyword:

  • In the class definition, which means that the class will expose only static elements and cannot be instantiated
  • In a member of a class, which means that an element can be called without having to initialize the class.
  • In the constructor, which means that the static constructor will be called before the static member is called. (Basically these are performance bottlenecks, and therefore not recommended).

Hope this helps.

+1
Mar 06 '09 at 19:45
source share

Wikipedia summarizes many different static values:

Static methods , static variables , static typing .

0
Mar 06 '09 at 19:57
source share



All Articles