When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable.
Important points for static variables:
- We can create static variables at class-level only. See here
- static block and static variables are executed in order they are present in a program.
Below is the java program to demonstrate that static block and static variables are executed in the order they are present in a program.
from m1 Inside static block Value of a : 20 from main
- Understanding storage of static methods and static variables in Java
- Static and non static blank final variables in Java
- Difference between static and non-static variables in Java
- Are static local variables allowed in Java?
- Assigning values to static final variables in Java
- Initialize a static map in Java with Examples
- Static and Dynamic data structures in Java with Examples
- Atomic Variables in Java with Examples
- Understanding "static" in "public static void main" in Java
- Why non-static variable cannot be referenced from a static method in Java
- Difference between static and non-static method in Java
- Internal static variable vs. External static variable with Examples in C
- Static import in Java
- Static blocks in Java
- static keyword in java
- Static class in Java
- Initialize a static Map using Java 9 Map.of()
- Static method in Interface in Java
- Final static variable in Java
- Shadowing of static functions in Java