Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

static — class-level member

static — class-level member: static associates a field, method, initializer, or nested class with the declaring class rather than a particular instance.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

static

static associates a field, method, initializer, or nested class with the declaring class rather than a particular instance.

Use static final for constants, a static factory for controlled construction, and a static nested class when a helper type needs no outer instance.

A static method has no this or super and remains statically selected even when called through an instance expression; mutable static state needs safe publication and synchronization.

Static initialization runs once during class initialization under JVM coordination; interface fields are implicitly public static final, but static methods are not inherited.

The static initializer sets the counter to 10, two constructors raise it to 12, and the static nested class identifies owner Main.

Examples

public class Main {
    static int count;
    static { count = 10; }
    static class Nested {
        static String owner() { return Main.class.getSimpleName(); }
    }
    Main() { count++; }

    public static void main(String[] args) {
        new Main();
        new Main();
        System.out.println(count);
        System.out.println(Nested.owner());
    }
}

Result

12
Main

The static initializer sets the counter to 10, two constructors raise it to 12, and the static nested class identifies owner Main.

Sources