Mutable Global State (ARCH4187)
هذا المحتوى غير متوفر بلغتك بعد.
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.
Incorrect
Section titled “Incorrect”public class Counter { public static int total = 0;
void increment() { total++; }}Correct
Section titled “Correct”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.