Static members
What you will learn
A static member belongs to the class rather than to a particular instance. Use static methods for operations that do not need an object receiver, and static fields for genuinely shared class-level state.
class Counter { private static int created; private int clicks; Counter() { created++; } void click() { clicks++; } int clicks() { return clicks; } static int created() { return created; }}public class Main { public static void main(String[] args) { Counter a = new Counter(); Counter b = new Counter(); a.click(); a.click(); System.out.println(a.clicks()); System.out.println(b.clicks()); System.out.println(Counter.created()); }}Expected output:
202The click counts are independent; the creation count is shared by the class.
How it works
An instance field is duplicated for each instance, but a static field belongs to the defining class in its loading context. Access a static member through the class name, such as Counter.created(), to communicate its ownership. An instance method has a current receiver and can access instance fields and static members. A static method has no current receiver, so it cannot use this or directly refer to an instance field without an object reference. It can still receive an object as a parameter and use accessible members on that object. The constant static final int MAX_ITEMS = 10 is shared and cannot be reassigned; a final reference, however, would not make the referenced object immutable. Use constants or pure helpers when useful. Mutable static state couples callers and persists across instances; it can also require synchronization when multiple threads access it. The counter examples are single-threaded and small, not a concurrency-safe ID generator.
class Units { static int minutesToSeconds(int minutes) { return minutes * 60; }}public class Main { public static void main(String[] args) { System.out.println(Units.minutesToSeconds(3)); System.out.println(Units.minutesToSeconds(0)); }}Expected output:
1800No Units object is required. The example uses small nonnegative values; arbitrary int multiplication may overflow.
Check an edge case
class Policy { static final int MAX_ITEMS = 10; static boolean accepts(int count) { return count >= 0 && count <= MAX_ITEMS; }}public class Main { public static void main(String[] args) { System.out.println(Policy.MAX_ITEMS); System.out.println(Policy.accepts(10)); System.out.println(Policy.accepts(11)); }}Expected output:
10truefalseOne constant defines the inclusive upper bound for the whole class.
Common mistakes
Do not make an instance field static merely to silence a compiler error in main. That changes the ownership of the data. Construct an object and call its instance method. Avoid treating a mutable static counter as a thread-safe or globally unique ID source.
Practice
Write a Counter class with a private static int created, a private int clicks, a constructor incrementing created, a click() method, and clicks() and static created() accessors. Verify separate click counts and a shared number of instances in a fresh program.
Save each complete example as Main.java in a fresh directory, compile with javac --release 21 Main.java, then run java Main. Each example is independent. A local JDK is required; the ByteMind Java runner is not verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Static members quiz
Check understanding using Java 21-compatible examples and the stated assumptions.
Which field should count all instances in a simple single-threaded fixture?
How is a static method normally called?
Why can a static method not use this?
What does static final int MAX = 10 mean here?
Does incrementing a static int field with count++ make a concurrent counter safe?