Java

Java

Made by DeepSource

Static fields of the parent class should not be accessed through child class instances JAVA-W1030

Anti-pattern
Major
Autofix

Non-private static members of the parent class are accessible by child classes. However, it is a bad practice to do so, because it obscures where a value was actually declared. Always use only the declaring class to access static members.

Bad Practice

class SomeClass {
    static Object staticData = null;
}

class SomeChildClass {
    public void method() {
        // Accessing the static value declared within the parent class through the child class.
        SomeChildClass.staticData = this;
    }
}

Recommended

class SomeChildClass {
    public void method() {
        // We are now accessing the static value through the parent class.
        SomeClass.staticData = this;
    }
}