-
Bug
-
Resolution: Fixed
-
P3
-
24
-
b03
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8348456 | 24.0.1 | Maurizio Cimadamore | P3 | Resolved | Fixed | b04 |
JDK-8346089 | 24 | Maurizio Cimadamore | P3 | Resolved | Fixed | b29 |
According to JEP 492 spec https://cr.openjdk.org/~gbierman/jep492/jep492-20241101/specs/flexible-constructor-bodies-jls.html,
If S is a local class whose declaration occurs in a static context, let N be the nearest static method declaration, static field declaration, or static initializer that encloses the declaration of S. If the nearest static method declaration, static field declaration, or static initializer that encloses the class instance creation expression is not N, then a compile-time error occurs.
So this code should fail, but it compiles:
class O {
static void foo(int x) { // static context, so no enclosing instance
class C { /* capture x */ }
class U {
static void test() {
class L extends C {
L() {
super();// compilation succeeds
}
}
}
}
}
}
java --version
java 24-ea 2025-03-18
Java(TM) SE Runtime Environment (build 24-ea+26-3339)
Java HotSpot(TM) 64-Bit Server VM (build 24-ea+26-3339, mixed mode, sharing)
If S is a local class whose declaration occurs in a static context, let N be the nearest static method declaration, static field declaration, or static initializer that encloses the declaration of S. If the nearest static method declaration, static field declaration, or static initializer that encloses the class instance creation expression is not N, then a compile-time error occurs.
So this code should fail, but it compiles:
class O {
static void foo(int x) { // static context, so no enclosing instance
class C { /* capture x */ }
class U {
static void test() {
class L extends C {
L() {
super();// compilation succeeds
}
}
}
}
}
}
java --version
java 24-ea 2025-03-18
Java(TM) SE Runtime Environment (build 24-ea+26-3339)
Java HotSpot(TM) 64-Bit Server VM (build 24-ea+26-3339, mixed mode, sharing)
- backported by
-
JDK-8346089 JEP 492: extending local class in a different static context should not be allowed
-
- Resolved
-
-
JDK-8348456 JEP 492: extending local class in a different static context should not be allowed
-
- Resolved
-
- relates to
-
JDK-8345953 JEP 492: instantiating local classes in a different static context should not be allowed
-
- Resolved
-
- links to
-
Commit(jdk24) openjdk/jdk/67b82516
-
Commit(master) openjdk/jdk/0ad64234
-
Review(jdk24) openjdk/jdk/22708
-
Review(master) openjdk/jdk/22679
(2 links to)