Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8254321

allow static nested declarations

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Approved
    • Fix Version/s: 16
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • Compatibility Kind:
      source
    • Compatibility Risk:
      minimal
    • Interface Kind:
      Language construct
    • Scope:
      SE

      Description

      Summary

      Relax language restrictions that prohibit static members declared by inner classes.

      Problem

      Since nested classes were first introduced to Java, nested class declarations that are inner have been prohibited from declaring static members, with the exception of static final fields initialized by constant expressions. This restriction applies to non-static member classes, local classes, and anonymous classes. It simplifies the language's task of resolving and validating references to in-scope variables, methods, etc.

      In JEP 384, the second preview of Record Classes added support for local interfaces, enum classes, and record classes. This was a well-received enhancement, permitting coding styles that reduce the scope of certain declarations to local contexts. It required enhancing the language so that references to variables, types, methods, and this are appropriately restricted when appearing anywhere within a nested static construct.

      While JEP 384 allowed for static local classes and interfaces, it did not relax the restriction on static member classes and interfaces of inner classes. So an inner class can declare a static interface inside one of its method bodies, but not as a class member.

      As a natural next step, JEP 395 proposes further relaxing nesting restrictions, permitting static classes, methods, fields, etc., to be declared within inner classes. The language changes in JEP 384 already address the problems of how variable and method references, etc., behave in these contexts, so all that needs changing is a removal of the restriction.

      Solution

      Remove restrictions that disallow static declarations in inner classes, which include non-static member classes, local classes, and anonymous classes. Newly-allowed declarations include:

      • static fields
      • static methods
      • static member classes and interfaces
      • static initializers

      Nested static declarations have no access to enclosing instances, local variables, or type parameters. These rules have already been established by JEP 384 to handle local interfaces, enums, and records.

      Because inner classes can inherit static members, this has no impact on the language's treatment of qualified member accesses occurring elsewhere.

      This change has no impact on existing programs—it expands the set of legal programs, but does not make any existing programs illegal or change their behavior.

      Specification

      The proposed change to the specification are discussed in detail in the document on Local and Nested Static Declarations. See also JDK-8253374.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              vromero Vicente Arturo Romero Zaldivar
              Reporter:
              vromero Vicente Arturo Romero Zaldivar
              Reviewed By:
              Gavin Bierman
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: