Java

Java

Made by DeepSource

Injected fields should not be assigned in injection constructors JAVA-W1079

Anti-pattern
Critical

Avoid modifying fields that are already annotated with @Inject inside an @Inject annotated constructor.

Bad Practice

Here, thing is marked as injected. However, Example's constructor is also marked with @Inject. This is redundant and could cause issues when initializing thing through both constructor and field injection.

class Example {
    @Inject
    String thing;

    @Inject
    public Example(String s) {
        thing = s; // thing is already supposed to be injected!
    }
}

Recommended

Only assign fields that are not marked as @Inject in the constructor.

class Example {
    @Inject
    String thang;

    String thing;

    @Inject
    public Example(String s) {
        thing = s;
    }
}

References