Add jep513 module: flexible constructor bodies on JDK 25 and 27

Runnable sources, broken examples and captured transcripts for the ankurm.com JEP 513 article, including the JDK 25 vs 27 compiler wording differences.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TF9JWFvJSNm6HVzswzZU5a
This commit is contained in:
Claude
2026-09-24 10:42:36 +00:00
parent e0cb6e83e2
commit 2ebea58d39
30 changed files with 510 additions and 0 deletions
@@ -0,0 +1,8 @@
class Parent { Parent() {} }
class AssignFieldWithInitializer extends Parent {
int size = 10; // has an initializer, which runs AFTER super()
AssignFieldWithInitializer() {
this.size = 20; // assignment in the prologue
super();
}
}
+7
View File
@@ -0,0 +1,7 @@
class Parent { int shared; Parent() {} }
class AssignInheritedField extends Parent {
AssignInheritedField() {
this.shared = 7; // a field the SUPERCLASS declares
super();
}
}
+8
View File
@@ -0,0 +1,8 @@
class Parent { Parent(int x) {} }
class CallMethodBeforeSuper extends Parent {
int compute() { return 42; }
CallMethodBeforeSuper() {
int v = compute(); // instance method call before super()
super(v);
}
}
+7
View File
@@ -0,0 +1,7 @@
class Base { Base(Runnable r) {} }
class InnerOuterThis extends Base {
int x = 3;
InnerOuterThis() {
super(() -> IO.println(x)); // lambda capturing this.x inside the super(...) arguments
}
}
+8
View File
@@ -0,0 +1,8 @@
class Parent { Parent(Object o) {} }
class NewInnerBeforeSuper extends Parent {
class Inner {}
NewInnerBeforeSuper() {
Inner i = new Inner(); // an inner class instance needs an enclosing this
super(i);
}
}
+14
View File
@@ -0,0 +1,14 @@
// Written with System.out so that JDK 21 can compile it: the ONLY thing wrong with it on 21 is the statement before super(...).
public class OnJdk21 {
static class Person { Person(String n) {} }
static class Employee extends Person {
Employee(String name, int salary) {
if (salary < 0) throw new IllegalArgumentException("negative");
super(name.strip());
}
}
public static void main(String[] args) {
new Employee(" a ", 1);
System.out.println("constructed");
}
}
+7
View File
@@ -0,0 +1,7 @@
class Parent { Parent(Object o) {} }
class PassThisBeforeSuper extends Parent {
PassThisBeforeSuper() {
Object self = this; // 'this' escapes before super()
super(self);
}
}
+8
View File
@@ -0,0 +1,8 @@
class Parent { Parent(int x) {} }
class ReadFieldBeforeSuper extends Parent {
int size = 10;
ReadFieldBeforeSuper() {
int copy = size; // reads this.size before super()
super(copy);
}
}
+9
View File
@@ -0,0 +1,9 @@
class Parent { Parent() {} }
class ReadFinalBeforeAssign extends Parent {
final String label;
ReadFinalBeforeAssign(String s) {
String t = this.label; // reading a field, even a final one, in the prologue
this.label = s;
super();
}
}
+7
View File
@@ -0,0 +1,7 @@
class Parent { Parent(int x) {} }
class ReturnBeforeSuper extends Parent {
ReturnBeforeSuper(int x) {
if (x < 0) return; // cannot return before the super call has completed
super(x);
}
}
+6
View File
@@ -0,0 +1,6 @@
class Parent { Parent(int x) {} }
class SuperInBranch extends Parent {
SuperInBranch(boolean big) {
if (big) super(100); else super(1); // super(...) must appear once, at top level of the constructor body
}
}
+7
View File
@@ -0,0 +1,7 @@
class Parent { Parent(int x) {} Parent() {} }
class TwoSuperCalls extends Parent {
TwoSuperCalls() {
super(1);
super(); // a second explicit constructor call
}
}