Skip to content

Commit 1881fba

Browse files
committed
JDK 25: update notes and example
1 parent e3487cd commit 1881fba

4 files changed

+170
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ A project to explore more about the new features from Java 8 through Java 21.
1616
* Remove the 32-bit x86 Port
1717
* Module Import Declarations
1818
* Compact Source Files and Instance Main Methods
19+
* Flexible Constructor Bodies
20+
* Structured Concurrency (preview 5)
1921

2022
* [Java 24](java-24/) (Mar, 2025)
2123
* Generational Shenandoah (experimental)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* To run: `java FlexibleConstructorBodiesExample.java`
3+
*/
4+
public class FlexibleConstructorBodiesExample {
5+
6+
public static void main(String[] args) {
7+
System.out.println("Pet:\n" + new Dog("Shih Tzu"));
8+
}
9+
}
10+
11+
abstract class Animal {
12+
protected final String domain;
13+
protected final String kingdom;
14+
protected final String phylum;
15+
protected final String subphylum;
16+
protected final String phylumClass;
17+
protected final String family;
18+
19+
private final String representation;
20+
21+
protected Animal(String phylum, String subphylum, String phylumClass, String family) {
22+
this.domain = "Eukaryota";
23+
this.kingdom = "Animalia";
24+
this.phylum = phylum;
25+
this.subphylum = subphylum;
26+
this.phylumClass = phylumClass;
27+
this.family = family;
28+
29+
this.representation = getClassification();
30+
}
31+
32+
public abstract String getClassification();
33+
34+
public String toString() {
35+
return this.representation;
36+
}
37+
}
38+
39+
class Dog extends Animal {
40+
private final String order;
41+
private final String species;
42+
private final String breed;
43+
44+
Dog(String breed) {
45+
// we can perform validation (fail fast)
46+
if (breed == null || breed.isBlank()) {
47+
throw new IllegalArgumentException("Breed cannot be null or empty");
48+
}
49+
this.order = "Carnivora";
50+
this.species = "Dog";
51+
this.breed = breed;
52+
super("Chordata", "Vertebrata", "Mammals", "Canidae");
53+
}
54+
55+
public String getClassification() {
56+
return "Domain: %s / Kingdom: %s / Phylum: %s / Subphylum: %s / Class: %s / Order: %s/ Species: %s / Breed: %s".formatted(
57+
domain, kingdom, phylum, subphylum, phylumClass, order, species, breed
58+
);
59+
}
60+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* To run: `java FlexibleConstructorBodiesInnerClassExample .java`
3+
*/
4+
public class FlexibleConstructorBodiesInnerClassExample {
5+
public static void main(String[] args) {
6+
var e1 = new Example1();
7+
var i1 = e1.new Inner1();
8+
9+
var e2 = new Example2();
10+
11+
var e3 = Example3.VALUE_3;
12+
}
13+
}
14+
15+
class Base {
16+
Base() {
17+
System.out.println("Base constructor");
18+
}
19+
}
20+
21+
class Example1 {
22+
private int counter;
23+
24+
void hello() {
25+
System.out.println("Hello from Example1");
26+
}
27+
28+
class Inner1 extends Base {
29+
Inner1() {
30+
// allowed cause when a new instance is created, the outer already exists
31+
Example1.this.counter++;
32+
hello();
33+
super();
34+
System.out.println("Inner1 constructor");
35+
}
36+
}
37+
38+
Example1() {
39+
// Error: cannot reference this before supertype constructor has been called
40+
// new Inner1();
41+
42+
super();
43+
}
44+
}
45+
46+
class Example2 {
47+
static class Inner2 extends Base {
48+
Inner2() {
49+
System.out.println("Inner2 constructor");
50+
}
51+
}
52+
53+
Example2() {
54+
System.out.println("Example2 constructor");
55+
new Inner2();
56+
super();
57+
}
58+
}
59+
60+
enum Example3 {
61+
VALUE_1(1), VALUE_2(2), VALUE_3(3);
62+
63+
private final int value;
64+
private final int valueSquared;
65+
66+
Example3(int v, int vSquared) {
67+
System.out.printf("Enum - %d ^ 2 = %d%n", v, vSquared);
68+
this.value = v;
69+
this.valueSquared = vSquared;
70+
}
71+
72+
Example3(int v) {
73+
var vv = v * v;
74+
this(v, vv);
75+
}
76+
77+
public int getValue() {
78+
return value;
79+
}
80+
81+
public int getValueSquared() {
82+
return valueSquared;
83+
}
84+
}

java-25/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ To run each example use: `java --enable-preview --source 25 <FileName.java>`
66

77
* [502](https://openjdk.org/jeps/502) - Stable Values (Preview)
88
* [503](https://openjdk.org/jeps/503) - Remove the 32-bit x86 Port
9+
* [505](https://openjdk.org/jeps/505) - Structured Concurrency (Fifth Preview)
910
* [511](https://openjdk.org/jeps/511) - Module Import Declarations
1011
* [512](https://openjdk.org/jeps/512) - Compact Source Files and Instance Main Methods
12+
* [513](https://openjdk.org/jeps/513) - Flexible Constructor Bodies
1113

1214
## Features
1315

@@ -89,6 +91,28 @@ To run each example use: `java --enable-preview --source 25 <FileName.java>`
8991
* provides methods: `print(Object)`, `println(Object)`, `println()`, `readln(String)` and `readln()`
9092
* this class uses return from `System.console()` to print and read from the default input and output streams
9193
* we can use with: `IO.println("Hello World")`
94+
* **Flexible Constructor Bodies**
95+
* promotion to standard without change
96+
* allow statements that do not reference the instance being created to appear before an explicit constructor invocation
97+
* will allow to use statements that use, transform or verify values before call `super`
98+
* the code before the `super` lives in pre-construction context:
99+
* can perform any statements that don't use any member of the instance being created (or its hierarchy or outter/inner classes that depends on the instance)
100+
* we can:
101+
* can access static members
102+
* initialize fields in the same class before explicitly invoking a constructor
103+
* this will allow a superclass never executes code with use subclass values with their default value (`0`, `false` or `null`)
104+
* if the class is an inner class, it can access members of its enclosing class (like `Outer.this.field++`)
105+
* the outer class instance already exists
106+
* inicialization flow:
107+
```
108+
C prologue
109+
--> B prologue
110+
--> A prologue
111+
--> Object constructor body
112+
--> A epilogue
113+
--> B epilogue
114+
C epilogue
115+
```
92116

93117
## Links
94118

0 commit comments

Comments
 (0)