1. Introduction
The static keyword in Java marks a member (field, method, block, or nested class) as belonging to the class itself rather than to any individual object. Static members are created once when the class is loaded into memory and are shared by all instances of that class.
Cricket analogy: The static keyword is like a franchise's team logo — it belongs to the team itself, not to any individual player, and is created once when the team is formed, shared identically by every player wearing that jersey.
Because static members exist independently of any object, they can be accessed using the class name directly, without needing to create an object first. This makes static ideal for constants, counters, and utility methods.
Cricket analogy: Because a league's official rulebook belongs to the league, you reference it directly as 'ICC Rules' without needing a specific match — just as static members are accessed via the class name directly, ideal for constants like MAX_OVERS, counters like totalMatchesPlayed, and utility methods like calculateNRR().
2. Syntax
class ClassName {
static dataType staticField;
static returnType staticMethod(parameters) {
// static method body
}
static {
// static initializer block, runs once at class loading
}
}
// Access without creating an object
ClassName.staticField = value;
ClassName.staticMethod();3. Explanation
A static field is shared across all objects of a class — if one object changes its value, every other object (and the class itself) sees the updated value, since there is only one copy in memory. Static methods can be called using the class name (e.g., Math.sqrt(4)), and they cannot directly access non-static (instance) fields or methods, because a static method may be called without any object existing at all.
Cricket analogy: A static field like totalTeamWins is shared by every player object — if one player's stats update it, the whole team sees the new value, since there's only one scoreboard; but a static method like Team.announceSchedule() can't reach a specific player's individual battingAverage, because the schedule can be announced before any player is even signed.
A static block is executed exactly once, when the class is first loaded by the JVM, and is typically used to initialize static fields that require more complex setup logic than a simple assignment.
Cricket analogy: A static block is like a stadium's one-time pitch preparation ritual done only when the venue is first certified for international matches — used to set up complex ground conditions before the first ball is ever bowled there.
Exam trap: A static method cannot directly reference this or call instance (non-static) methods/fields, because static methods can execute without any object instance existing. Doing so causes a compile-time error.
Static initializer blocks run once, in the order they appear, when the class is loaded — before any object of the class is created or any static method is called.
4. Example
public class Counter {
static int count = 0; // shared across all objects
int id;
static {
System.out.println("Counter class loaded");
}
Counter() {
count++;
id = count;
}
static void showCount() {
System.out.println("Total objects created: " + count);
// count is static, so it's accessible directly here
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
System.out.println("c1 id: " + c1.id);
System.out.println("c2 id: " + c2.id);
System.out.println("c3 id: " + c3.id);
Counter.showCount();
}
}5. Output
Counter class loaded
c1 id: 1
c2 id: 2
c3 id: 3
Total objects created: 36. Key Takeaways
- Static members belong to the class, not to any individual object, and are shared by all instances.
- Static members are accessed via ClassName.member, though ObjectRef.member also works (not recommended).
- Static methods cannot directly access non-static (instance) fields or methods.
- Static blocks execute once, when the class is loaded by the JVM.
- Static fields are a single shared copy in memory across all objects of the class.
Practice what you learned
1. What does marking a field `static` mean?
2. Which of the following can a static method NOT do directly?
3. When does a static initializer block execute?
4. How is a static method typically invoked?
5. In the Counter example, why do c1, c2, c3 get ids 1, 2, 3 respectively?
Was this page helpful?
You May Also Like
Constructors in Java
Understand Java constructors — default, parameterized, overloaded, and chained — with syntax rules and worked examples.
this Keyword in Java
Master the `this` keyword in Java — resolving field/parameter name conflicts, constructor chaining, and passing the current object.
Classes and Objects in Java
Learn how classes act as blueprints and objects as instances in Java, with syntax, examples, and exam-focused explanations.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics