Proposed Rule Name:
InnerClassMayBeStatic
Proposed Category:
Code Style
Description:
Inner classes that are final or private and do not reference fields or methods of their top level class can be static.
For Java <18 making them static avoids creating a synthetic reference to the parent object, improving performance (see https://www.oracle.com/java/technologies/javase/18-relnote-issues.html#JDK-8271623).
For Java 18 and later this rule might still be useful to clearly distinguish classes that are intended to reference their parent and classes that are not.
Code Sample:
class A {
int foo = 4;
class B { // may be a static class
}
}
Possible Properties:
IgnoreAnnotations to skip this rule for annotated inner classes (e.g. org.junit.jupiter.api.Nested might be a good exception)
Related checks
Proposed Rule Name:
InnerClassMayBeStaticProposed Category:
Code Style
Description:
Inner classes that
are final or private anddo not reference fields or methods of their top level class can be static.For Java <18 making them static avoids creating a synthetic reference to the parent object, improving performance (see https://www.oracle.com/java/technologies/javase/18-relnote-issues.html#JDK-8271623).
For Java 18 and later this rule might still be useful to clearly distinguish classes that are intended to reference their parent and classes that are not.
Code Sample:
Possible Properties:
IgnoreAnnotationsto skip this rule for annotated inner classes (e.g.org.junit.jupiter.api.Nestedmight be a good exception)Related checks