Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

The static keyword

Reference for the Java static keyword: static fields and methods, static nested classes, static initializer blocks, static imports, and common pitfalls.

static attaches a member to a class instead of to each object. A static field exists once per class, no matter how many instances you create. A static method is called on the class and has no this. A static nested class does not hold a reference to an enclosing instance.

Static members are loaded and initialized with the class, which makes them a good fit for constants, factory methods, utilities and counters, and a poor fit for mutable shared state in concurrent programs.

Java
class Counter {
    static int created;            // one copy for the whole class
    static { created = 0; }        // static initializer block
    Counter() { created++; }
    static int total() { return created; }
}

Try it

Java Static field and method

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Initializer order and static nested class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static Type field;                  // static field
static ReturnType name(params) { }  // static method
static { /* runs once, at class initialization */ }
static class Nested { }             // static nested class
import static java.lang.Math.max;   // static import
Form Meaning
Static field Shared by all instances. Reachable as ClassName.field.
Static method Cannot use this, super or instance members directly. Cannot be overridden (only hidden).
Static block Runs once, in textual order with static field initializers, when the class is initialized.
Static nested class Ordinary class scoped inside another; sees the outer class's static members.
Static import Lets you use a static member by simple name.

Usage notes

  • When initialization happens: the first time the class is used (instance creation, static method call, non-constant static field access), not when the program starts.
  • Instance access warning: ticket.issued() compiles but is misleading; call it as Ticket.issued(). Compilers and linters flag it.
  • No overriding: a subclass static method with the same signature hides the parent's. Which one runs depends on the declared type, not the object.
  • Thread safety: static mutable fields are shared by every thread. Guard them with synchronized, atomics or concurrent collections, or avoid them.
  • Memory: statics live as long as the class (and its class loader). A static collection that only grows is a classic memory leak.
  • Testing: static state and static calls are hard to replace in tests; prefer passing dependencies in.
  • Interfaces: may declare static methods (since Java 8). They are not inherited by implementing classes.
  • Main method: the traditional public static void main(String[] args) is static so the JVM can call it without creating an object. Java 25 also accepts an instance void main().
  • Local records, enums and interfaces are implicitly static.