static
In short: static binds an element to the class instead of to a single object.
In more detail: Static attributes and methods exist only once per class and are called via the class name.
In Depth
class Counter {
static int count = 0; // belongs to the class
static int next() { return ++count; }
}
int n = Counter.next(); // without an objectNotes
- Static methods cannot use object attributes directly (no this).
Math.max()andmainare static.- Excessive use makes code hard to test.
See class members, final.
See also: Class Members, final, Meaning of this