java / beginner
Snippet
The Static Keyword
Static members belong to the class itself rather than individual instances, meaning they are shared by all objects.
snippet.java
1
2
3
4
5
6
7
class Counter {static int count = 0;Counter() {count++;}}
Breakdown
1
static int count = 0;
Declares a variable that is shared across all instances of Counter.
2
count++;
Increments the shared class variable every time a new object is created.