Skip to content

Commit 59ecf23

Browse files
committed
JEP 513: Flexible Constructor Bodies
1 parent 676e973 commit 59ecf23

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ This repository contains Java examples that are designed to track and document t
1111
## Specifications & Practices
1212

1313
* [Java 25](java-25/) (September, 2025)
14-
* [JEP 512](java-25/src/main/java/JEP512CompactSourceFilesAndInstanceMainMethods.java): Compact Source Files and Instance Main Methods
14+
* [JEP 513](java-25/src/main/java/JEP513FlexibleConstructorBodies.java): Flexible Constructor Bodies
15+
* [JEP 512](java-25/src/main/java/JEP512CompactSourceFilesAndInstanceMainMethods.java): Compact Source Files and Instance Main Methods
1516
* [JEP 511](java-25/src/main/java/JEP511ModuleImportDeclarations.java): Module Import Declarations
17+
1618
* [Java 24](java-24/) (March, 2025)
1719
* [JEP 488](java-24/src/main/java/JEP488PrimitiveTypesInPatternsInstanceofAndSwitch.java): Primitive Types in Patterns, instanceof, and switch
1820
* [JEP 495](java-24/src/main/java/JEP495SimpleSourceFilesAndInstanceMainMethods.java): Simple Source Files and Instance Main Methods
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// JEP 513: Flexible Constructor Bodies
2+
// https://openjdk.org/jeps/513
3+
4+
// Support JEP 513: Flexible Constructor Bodies
5+
// https://youtrack.jetbrains.com/projects/IDEA/issues/IDEA-372971/Support-JEP-513-Flexible-Constructor-Bodies
6+
7+
void main(String[] args) {
8+
Employee e1 = new Employee(30, "A123");
9+
e1.show();
10+
11+
// Employee Age: 30, Office ID: null
12+
// Employee Age: 30, Office ID: A123
13+
}
14+
15+
class Person {
16+
final int age;
17+
18+
Person(int age) {
19+
this.age = age;
20+
show();
21+
}
22+
23+
void show() {
24+
System.out.println("Age: " + age);
25+
}
26+
}
27+
28+
class Employee extends Person {
29+
final String officeId;
30+
31+
Employee(int age, String officeId) {
32+
super(validateAge(age));
33+
this.officeId = officeId;
34+
}
35+
36+
private static int validateAge(int age) {
37+
if (age < 18 || age > 67)
38+
throw new IllegalArgumentException("Invalid age: " + age);
39+
return age;
40+
}
41+
42+
@Override
43+
void show() {
44+
System.out.println("Employee Age: " + age + ", Office ID: " + officeId);
45+
}
46+
}

0 commit comments

Comments
 (0)