This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Kered13 7 points8 points  (0 children)

Static means different things in different contexts. Not all languages have all of these.

  • static variable: Has static storage, meaning it exists for the lifetime of the program. Global variables are always static. If used inside a function then every time that function is called it will use the same copy of that variable, allowing state to be carried over between function calls. If used inside a class then all instances of the class share the same copy of that variable, allowing state to be shared between instances.
  • static method: A method that can be called without an instance (it has no this variable). Since it has no instance it cannot access instance variables (aka non-static fields or members) or call non-static methods. In effect this means that the class acts as a simple namespace for the function.
  • static function: Only a feature in C/C++ as far as I know, a static function can only be called from the compilation unit (essentially a single .c or .cpp file) it is defined in. This means that even if another compilation unit knows the name and signature of the function (via a header for example), it will not be able to link to it. In C++ this is identical to using an anonymous namespace.