A static variable in Java is a class-level variable that is declared using the static keyword. Unlike instance variables, only one copy of a static variable is created and shared among all objects of the class, making it useful for storing common data.
- Declared using the static keyword and belongs to the class rather than individual objects.
- A single copy of the variable is shared by all instances of the class.
- Commonly used for constants, configuration values, counters, and data shared across all objects.
class Geeks{
static int number = 100;
public static void main(String[] args) {
System.out.println(number);
number = 200;
System.out.println(number);
}
}
Output
100 200
Explanation: In this example, number is a static variable. It belongs to the Test class and is accessed directly inside the main() method without creating any object. Initially, its value is 100. After updating it to 200, the new value is printed. This demonstrates that a static variable is a class-level variable whose value can be accessed and modified throughout the class.
Declaration Syntax
class ClassName {
static dataType variableName;
}
Why Use Static Variables?
Static variables are useful when the same value needs to be shared among all objects of a class.
- Maintaining shared application state.
- Sharing common information among all instances.
Important Points
- Static variables can only be declared at the class level (not inside methods).
- They are initialized when the class is loaded into memory.
- All objects share the same copy of a static variable.
- Static variables can be accessed using the class name without creating an object.
- If one object modifies a static variable, the updated value becomes visible to every other object.
Example to demonstrates how to use a static variable among different methods.
In this example, the static variable a is initialized by calling the m1() method. When the class is loaded, Java first initializes the static variable, then executes the static block, and finally calls the main() method.
class Geeks
{
// static variable
static int a = m1();
// static block
static
{
System.out.println("Inside static block");
}
// static method
static int m1()
{
System.out.println("from m1");
return 20;
}
// static method(main)
public static void main(String[] args)
{
System.out.println("Value of a : " + a);
System.out.println("from main");
}
}
Output
from m1 Inside static block Value of a : 20 from main
Explanation:
- The class is loaded into memory.
- The static variable a is initialized by calling m1().
- The static block executes.
- Finally, the main() method runs and prints the value of a.
Static variable vs Instance variable
| Aspect | Static Variable | Instance Variable |
|---|---|---|
| Belongs To | Class | Object (Instance) |
| Memory Allocation | Created once when the class is loaded | Created whenever an object is created |
| Number of Copies | One shared copy | Separate copy for every object |
| Access | Using the class name (recommended) or an object (not recommended). | Using object reference |
| Modification | Changes are reflected across all objects | Changes affect only that specific object |
| Lifetime | Until the class is unloaded | Until the object is garbage collected |
| Common Uses | Constants, counters, shared data | Object-specific properties |