BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Language basics

Java language

static and final members

static means "belongs to the class, not to any object": one shared copy.

Learn these first

At a glance

Mental model

A static field is the notice board in the lobby that everyone in the building sees and edits; an instance field is the note on your own desk.

What happens, in order
  1. 1A static field has one copy for the whole class (loaded once), reachable as Config.MAXA static method has no this and cannot use instance fields.
  2. 2A final field or variable can be assigned only onceA final reference cannot point elsewhere, but the object it points to can still change (final List can still add).
  3. 3static final with a literal value is a compile-time constant and is inlined into every class that uses itName constants in UPPER_SNAKE_CASE.
Remember
Watch out
Using a mutable static field as a shortcut for passing dataThinking final makes a collection immutableCalling an instance method from a static method without an object
Example (highlighted lines explain it)
public class Counter {  public static final int MAX = 100;  private static int created;  private final int id;  public Counter() { id = ++created; }  public static int created() { return created; }}
Read the full explanation (about 1 min)

static means "belongs to the class, not to any object": one shared copy. final means "assigned once, never changed". Together (static final) they make constants.

A mental model

A static field is the notice board in the lobby that everyone in the building sees and edits; an instance field is the note on your own desk. final is a note written in ink.

How it works

A static field has one copy for the whole class (loaded once), reachable as Config.MAX. A static method has no this and cannot use instance fields.

A final field or variable can be assigned only once. A final reference cannot point elsewhere, but the object it points to can still change (final List can still add).

static final with a literal value is a compile-time constant and is inlined into every class that uses it. Name constants in UPPER_SNAKE_CASE.

Going deeper

static mutable state is global state: every thread and every test shares it, so it is a common source of race conditions and tests that pass alone but fail together. Prefer passing dependencies in.

Static initialisation order matters when static fields depend on each other. Constants of another class that are compile-time constants get copied: change the constant and you must recompile the users.

Under the hood

Statics live in the class object (metaspace for the class, heap for the objects they reference), so they exist as long as the class loader that loaded the class exists. Static fields keep their objects alive and are classic memory-leak roots.

Common mistakes

  • Using a mutable static field as a shortcut for passing data.
  • Thinking final makes a collection immutable.
  • Calling an instance method from a static method without an object.

How Unravel recognises it

When you paste code, Unravel looks for these signs of static and final members. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Builds on this

Frequently asked questions

What is static and final members?

static means "belongs to the class, not to any object": one shared copy. final means "assigned once, never changed". Together (static final) they make constants.

What are the most common mistakes with static and final members?

Using a mutable static field as a shortcut for passing data. Thinking final makes a collection immutable. Calling an instance method from a static method without an object.

How can I see static and final members in real code?

Paste your own Java into Unravel and it will point out where static and final members appears, explain how it works at the detail level you choose, and draw the flow of the code around it.