Zum Inhalt springen

Mutable Global State (ARCH4187)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key java.quality.mutable-static-field · Severity warning · Category quality

Mutable static fields create global, shared state that leads to concurrency bugs and broken test isolation. Move the state to instance scope or make it final.

public class Counter {
public static int total = 0;
void increment() { total++; }
}
public class Counter {
private final AtomicInteger total = new AtomicInteger();
void increment() { total.incrementAndGet(); }
}

Turn the shared mutable static field into an instance field; if it is truly constant, make it static final.