From ab63837b1bf5ad428ba9e96af2890b38ae8e6c39 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 21:29:50 +0000 Subject: [PATCH 1/2] Add Java 25 flexible constructor bodies examples --- .../test/java/pl/mperor/lab/java/Java25.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java b/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java index 746a43d..6e1ae7b 100644 --- a/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java +++ b/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java @@ -38,4 +38,64 @@ public void testPrintFromIOClass() { TestUtils.resetSystemOut(); } + @Test + public void testValidateArgumentBeforeSuperCall() { + Assertions.assertEquals(5, new PositiveBox(5).value); + Assertions.assertThrows(IllegalArgumentException.class, () -> new PositiveBox(-1)); + } + + @Test + public void testFieldAssignedBeforeSuperVisibleToOverriddenMethod() { + var circle = new Circle(2.5); + Assertions.assertEquals("circle r=2.5", circle.description); + } + + static class Box { + final int value; + + Box(int value) { + this.value = value; + } + } + + /// Before JEP 513, validating/transforming `value` required a private static + /// helper method invoked inside `super(...)`, since no statement could precede it. + static class PositiveBox extends Box { + PositiveBox(int value) { + if (value <= 0) { + throw new IllegalArgumentException("value must be positive: " + value); + } + super(value); + } + } + + static class Shape { + Shape() { + describe(); // an overridable method invoked from the superclass constructor + } + + void describe() { + } + } + + static class Circle extends Shape { + final double radius; + String description; + + Circle(double radius) { + // Still forbidden in the prologue (would not compile here, before super()): + // System.out.println(this); // cannot reference `this` + // describe(); // cannot call an instance method + // System.out.println(radius); // cannot read an own field back + this.radius = radius; // JEP 513: assigning an own field is now allowed before super() + super(); + } + + @Override + void describe() { + // radius is already set here, even though super() has not returned yet + description = "circle r=" + radius; + } + } + } \ No newline at end of file From 613bca91c6c3c8e9f59a81b2074f3953f31ccdc2 Mon Sep 17 00:00:00 2001 From: mperor Date: Thu, 22 Oct 2026 23:45:07 +0200 Subject: [PATCH 2/2] Place helper classes directly below the tests that use them --- .../src/test/java/pl/mperor/lab/java/Java25.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java b/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java index 6e1ae7b..150dc8d 100644 --- a/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java +++ b/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java @@ -44,12 +44,6 @@ public void testValidateArgumentBeforeSuperCall() { Assertions.assertThrows(IllegalArgumentException.class, () -> new PositiveBox(-1)); } - @Test - public void testFieldAssignedBeforeSuperVisibleToOverriddenMethod() { - var circle = new Circle(2.5); - Assertions.assertEquals("circle r=2.5", circle.description); - } - static class Box { final int value; @@ -69,6 +63,12 @@ static class PositiveBox extends Box { } } + @Test + public void testFieldAssignedBeforeSuperVisibleToOverriddenMethod() { + var circle = new Circle(2.5); + Assertions.assertEquals("circle r=2.5", circle.description); + } + static class Shape { Shape() { describe(); // an overridable method invoked from the superclass constructor @@ -98,4 +98,4 @@ void describe() { } } -} \ No newline at end of file +}