0 day streak
java / beginner
Snippet

Static Variables

Static variables belong to the class itself rather than any specific object. All instances of the class share the same static variable.

snippet.java
java
1
2
3
4
5
6
class Counter {
static int count = 0;
Counter() {
count++;
}
}
Breakdown
1
static int count = 0;
Declares a static variable that is shared across all instances.
2
count++;
Increments the shared count whenever a new object is created.