Skip to content

[java] New rule: Inner class may be static #6711

@zbynek

Description

@zbynek

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-ruleProposal to add a new built-in rule

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions