The static keyword is used to declare class-level variables, methods, initialization blocks, and nested classes. Static members belong to the class rather than individual objects.
- Static variables share a single value across all instances of a class.
- Static methods can be called using the class name without creating an object.
- Static blocks initialize class-level data when the class is initialized.
Types of Static Members
The static keyword can be used with four main components.
1. Static Variables
A static variable belongs to the class and has one shared value across its instances.
class Student{
int rollNo;
String name;
// Shared static variable
static String trainingCenter = "GFG";
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
void display(){
System.out.println(rollNo + " " + name + " " + trainingCenter);
}
}
public class Geeks{
public static void main(String[] args){
Student s1 = new Student(101, "Ravi");
Student s2 = new Student(102, "Amit");
s1.display();
s2.display();
}
}
Output
101 Ravi GFG 102 Amit GFG
Explanation: Both Student objects access the same static variable trainingCenter, while their instance variables store different values.
2. Static Blocks
A static block executes when the class is initialized. It is commonly used to initialize static variables that require additional logic.
class Geeks{
static int a = 10;
static int b;
// Static initialization block
static{
b = a * 4;
System.out.println("Static block executed");
}
public static void main(String[] args){
System.out.println("Value of b: " + b);
}
}
Output
Static block executed Value of b: 40
Explanation: The static block executes during class initialization and assigns the value 40 to b before the main() method runs.
3. Static Methods
A static method belongs to the class and can be invoked without creating an object. It cannot directly access instance variables or methods without an object reference.
class Geeks{
// Static method
static void display(){
System.out.println("Hello, Geeks!");
}
public static void main(String[] args){
// Call using class name
Geeks.display();
}
}
Output
Hello, Geeks!
Explanation: The display() method is invoked directly using the class name because it is declared static.
4. Static Nested Classes
A static nested class is declared inside another class using the static keyword. Unlike a non-static inner class, it does not require an instance of the enclosing class.
class Outer{
static class Inner{
void show(){
System.out.println("Static Nested Class");
}
}
public static void main(String[] args){
// Create nested class object
Outer.Inner obj = new Outer.Inner();
obj.show();
}
}
Output
Static Nested Class
Explanation: The Inner class can be instantiated without creating an object of Outer.
Note: A static nested class is not a shared object. It is a nested type that can have multiple independent instances.
Static Vs Non-Static
Aspect | Static | Non-Static |
|---|---|---|
Belongs To | Class | Object |
Memory | One static field per declaring class | Separate instance fields for each object |
Access | Through class name | Through object reference |
Member Access | Cannot directly access instance members without an object | Can access instance and static members |
Common Uses | Utility methods, constants, shared data | Object-specific data and behavior |
Important Points
- Static methods cannot directly access instance variables or methods without an object reference.
- The this and super keywords cannot be used in a static context to refer to an instance.
- Static methods are hidden rather than overridden when a subclass declares a compatible static method.
- Static nested classes do not have an implicit reference to an instance of the enclosing class.