static keyword

When a variable in a class is declared static, space for it is allocated for the lifetime of the program.

No matter how many objects of that class have been created, there is only one copy of the static member. So same static member can be accessed by all the objects of that class.

Tip

A static member function can be called even if no objects of the class exist and the static function are accessed using only the class name and the scope resolution operator ::.

static can also be found in C.

When should you ever use the static keyword?

Depending on what scope you declare static, it will achieve different outcomes:

  • Static member fields: Use static to share a variable among all objects of a class; it belongs to the class and not any specific object.
  • Static member methods: Apply to member functions that can be called on the class itself rather than on instances of the class.
  • Static variable inside function: Declare a local variable as static to retain its value between function calls (ex: count number of function calls).
  • Static global functions and variables: Use static for global variables or functions to restrict their visibility to the current file.

More can be found and explained on StackOverflow.

Static Variables depending on each other??? #todo

Static fields and methods??

Python Static Method This article is super helpful: https://www.programiz.com/python-programming/methods/built-in/staticmethod