Query module InnerClassCouldBeStatic

name
Inner class could be static
description
A non-static nested class keeps a reference to the enclosing object, which makes the nested class bigger and may cause a memory leak.
kind
problem
problem.severity
recommendation
precision
high
id
java/non-static-nested-class
tags
efficiency maintainability

Imports

java

Provides all default Java QL imports.

Predicates

enclosingInstanceAccess
inherits

Is the field f inherited by the class c? This is a slightly imprecise, since package-protected fields are not inherited by classes in different packages, but it’s enough for the purposes of this check.

potentiallyStatic

A nested class c could be static precisely when

Classes

EnclosingInstanceAccess

An access to a method or field that uses an enclosing instance of the type containing it.

ProblematicClass

A problematic class, meaning a class that could be static but isn’t.