On this page
static
Prev: `final` | Up: builtins | Next: `try with resources`
Rel: builtins, Classes, Records & Sealed Hierarchies
Ref: Java Docs — static
Tags: #public #prog #java #builtins #tier/core
Availability
Built-in / java.lang (Language Modifier) — available with zero import statements.
Syntax & Overview
public static int add(int a, int b) { return a + b; }
Binds a field, method, or nested class to the class itself rather than an instance.
Notes & Pitfalls
- Belongs to the class, not an instance: Static methods cannot access
thisor instance fields. - Prefer
static class Nestedover non-static inner classes to avoid hidden references to the enclosing instance (a common memory leak).
Example
class Counter {
static int next = 0;
static int bump() { return ++next; }
}
assert Counter.bump() == 1;
Linked from
Related notes
- tuts/java/builtins/final Linked here; links to this page; shared tags: #builtins, #java, #prog, #tier
- tuts/java/builtins/try_with_resources Linked here; links to this page; shared tags: #builtins, #java, #prog, #tier
- tuts/java/builtins Linked here; links to this page; shared tags: #builtins, #java, #prog
- tuts/java/language_and_oop/classes_records_and_sealed_hierarchies Linked here; shared tags: #java, #tier
- tuts/java/builtins/AutoCloseable shared tags: #builtins, #java, #prog, #tier
- tuts/java/builtins/enum shared tags: #builtins, #java, #prog, #tier
- tuts/java/builtins/finalize shared tags: #builtins, #java, #prog, #tier
- tuts/java/builtins/interface shared tags: #builtins, #java, #prog, #tier
- tuts/java/builtins/Object shared tags: #builtins, #java, #prog, #tier
- tuts/java/builtins/Object_wait_notify shared tags: #builtins, #java, #prog, #tier