Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-429

Rule "Constants should be declared 'final static' rather than merely 'final'" generates some false-positives on inner classes

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.1
    • Component/s: Rules
    • Labels:
      None

      Description

      According to the Java specification:

      8.1.2 Inner Classes and Enclosing Instances

      An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (§8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (§15.28).

      public class NestingClass {
        public class InnerClass {
          private static final int POSSIBLE = 4;
          private static final String POSSIBLE_2 = "";
      
          private static final String[] NOT_POSSIBLE = {};
          private static final Object NOT_POSSIBLE_2 = new Object();
        }
      }
      

      So no issue should be reported on final field of inner classes whose type is not a primitive or a String. Rule specification has been updated.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              benzonico OLD - Nicolas Peru
              Reporter:
              freddy.mallet Freddy Mallet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: