Java

Java

Made by DeepSource

Static methods should be accessed using the class instance JAVA-W1029

Anti-pattern
Major
Autofix

While it is possible to access static members of a class through an instance of that class, it is a bad practice to do so.

Always access a static member through the declaring class itself, not an instance of the class.

Bad Practice

class SomeClass {
    static boolean somethingHappened = false;

    public void someMethod() {
        // Static value accessed through an object.
        this.somethingHappened = true;
    }
}

Recommended

Access the value through the class itself.

public void someMethod() {
        SomeClass.somethingHappened = true;
    }