Static Variable
TLDR: A static variable is a variable declared static with the `static` keyword in programming languages like Java, CPP, and C Sharp. Unlike instance variables, static variables are shared across all instances of a class and belong to the class itself rather than any specific object. They are initialized only once during the class loading phase and retain their variable value throughout the program's execution.
https://en.wikipedia.org/wiki/Static_variable
In Java, a static variable is declared using the syntax `static Type variableName;` within a class. For example, `static int counter;` allows all instances of the class to share a common `counter` value. Static variables are commonly used to store constants, such as `final static double PI = 3.14159;`, or shared resources like configuration settings. Accessing a static variable does not require an object instance; it can be referenced using `ClassName.variableName`.
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
While static variables provide memory efficiency and a shared state, improper use can lead to unintended side effects or tight coupling between class components. For instance, modifying a static variable in one instance affects all other instances that rely on it. Proper variable scoping and encapsulation of static variables help mitigate these risks, ensuring their effective use in program design.
https://docs.oracle.com/javase/specs/jls/se20/html/jls-8.html