-
CSR
-
Resolution: Approved
-
P3
-
source
-
minimal
-
Language construct
-
SE
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
fieldsstatic
methodsstatic
member classes and interfacesstatic
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.
- csr of
-
JDK-8254105 allow static nested declarations
- Resolved
- relates to
-
JDK-8246772 JLS changes for Record Classes
- Resolved
-
JDK-8253374 JLS changes for Static Members of Inner Classes
- Resolved
-
JDK-8291154 Create a non static nested class without enclosing class throws VerifyError
- Resolved
-
JDK-8264474 Java 16 release notes do not mention relaxed rules for static members
- Closed
-
JDK-8335005 The execution result is inconsistent with the document description
- Closed