Inner Class Should Be Static

ID

java.inner_should_be_static

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Efficiency

Language

Java

Tags

efficiency

Description

Reports non-static inner classes that never reach the enclosing class instance. A non-static inner class implicitly holds a reference to its enclosing instance, preventing garbage collection and increasing memory overhead.

Almost no inner class reaches the outer instance through Outer.this. It reads an outer field or calls an outer method by its bare name, and the enclosing instance is supplied silently. Every name and every unqualified call in the class is therefore resolved through the symbol table: the class is left alone when a reference resolves to an instance field or instance method declared outside it, when it instantiates another non-static member class of an enclosing type, or when it writes Outer.this.

Two shapes are skipped outright because nothing in the file can settle them: a local class, which cannot take static at all, and an inner class of a class that has a superclass, since instance members inherited from another file are not in the lexical scope the symbol table walks.

Rationale

Every non-static inner class instance carries a hidden reference to its enclosing outer class instance. If the inner class never uses this reference, the memory overhead is wasted and can cause memory leaks.

public class Outer {
    // Bad -- reaches nothing of Outer
    class Inner {
        public int compute() { return 42; }
    }
}
public class Outer {
    private JavaCommand esCommand;

    // OK -- returns an outer instance field by its bare name; 'static' would not compile
    class Inner {
        JavaCommand createEsCommand() { return esCommand; }
    }
}

Remediation

Declare the inner class static when it does not reference the enclosing instance.

public class Outer {
    static class Inner {
        public int compute() { return 42; }
    }
}

References

© 2026 Xygeni. All rights reserved