Skip to main content

avoid-assigning-to-static-field

added in: 1.22.0
Pro+

Warns when an instance method assigns to a static field.

An assignment to a static field from a regular method can either indicate a mutable global state that is shared between multiple instances (which in itself can lead to tricky bugs where one instance modifies the behavior of all other instances), or a field that is inadvertently marked as static. Consider refactoring the code or marking the method static.

Example

❌ Bad:

class Some {
static int value = 1;

void work() {
value = 2; // LINT: Avoid assigning to static fields from non-static methods.
}
}

✅ Good:

class Some {
static int value = 1;

static void work() {
value = 2; // Correct, the method is also static
}
}