What Does static Mean in Java?

static marks a field or method as belonging to the class itself, not to any particular object created from it. A static field has one copy shared by all instances; a static method can be called without creating an object.

Static fields

public class Counter {
    public static int count = 0;   // one copy for the whole class
    public int id;                 // each instance has its own id

    public Counter() {
        count++;
        this.id = count;
    }
}

Counter a = new Counter(); // count=1, a.id=1
Counter b = new Counter(); // count=2, b.id=2
System.out.println(Counter.count); // 2 β€” accessed on the class

Static methods

public class MathHelper {
    public static int square(int n) { return n * n; }
}

int result = MathHelper.square(5); // 25 β€” no object needed

Rules for static methods:

  • Cannot use this (there is no object context)
  • Cannot access instance fields or instance methods directly
  • Can access other static fields and static methods

The main method is static

public static void main(String[] args) { ... }

The JVM calls main without creating an instance of your class first β€” which is why it must be static. If it were an instance method, the JVM wouldn't know which instance to call it on.

Static constants

Combine static with final for a compile-time constant shared across the codebase:

public class Config {
    public static final int MAX_RETRIES = 3;
    public static final String DEFAULT_ENCODING = "UTF-8";
}

Static initializer blocks

Code that runs once when the class is first loaded, before any instances are created:

public class DbPool {
    private static final Connection connection;

    static {
        // runs once at class load time
        connection = DriverManager.getConnection("jdbc:...");
    }
}

Static inner classes

A static nested class is associated with the outer class type, not with any particular instance of it:

public class LinkedList {
    private static class Node {   // static inner class
        int value;
        Node next;
    }
}

Node doesn't need access to the outer list's instance fields, so it can be static β€” this avoids an implicit reference to the outer object and reduces memory usage.

Common gotcha: calling static methods on instances

Counter c = new Counter();
c.count;  // compiles but misleading β€” it is accessing Counter.count

Java allows calling static members on an instance reference, but the compiler generates a warning. Always use the class name for static access (Counter.count) to make the code intention clear.