Examples illustrating the most interesting developer-friendly features after Java 8 described in the following article: New Developer Friendly Features After Java 8
| Feature | Java version | Status | Source / test |
|---|---|---|---|
| Private interface methods | 9 | Final | privatemethods/ExampleInterface |
Immutable collection factories (List.of, Map.of) |
9 | Final | unmodifiablecollections/CollectionsCreator, test |
Optional improvements (orElseThrow, ifPresentOrElse) |
9-10 | Final | optional/OptionalMethods |
ServiceLoader / SPI |
9 | Final | spi/, test |
Local-variable type inference (var) |
10-11 | Final | localvar/LocalVar |
DelayQueue |
(classic) | Final | helper/DelayedEvent, test |
| Switch expressions | 14 | Final | switches/SwitchComparison, test |
| Text blocks | 15 | Final | textblocks/TextBlockString, test |
| Records | 16 | Final | records/, test |
| Records with MapStruct | 16 | Final | mapper/PersonMapper, test |
| Sealed classes | 17 | Final | sealedclasses/ |
HexFormat |
17 | Final | test |
Day period pattern B in DateTimeFormatter |
16 | Final | test |
Pattern matching for switch |
21 | Final | switches/SwitchComparison, test |
| Sequenced collections | 21 | Final | test |
| Virtual threads | 21 | Final | virtualthreads/ThreadComparison, test |
| Record patterns and nested deconstruction (JEP 440) | 21 | Final | records/RecordPatterns, test |
EnumSet (range, complementOf) |
(classic) | Final | test |
Unnamed variables and patterns _ (JEP 456) |
22 | Final | unnamed/UnnamedVariables, test |
| Stream Gatherers (JEP 485) | 24 | Final | gatherers/GatherersComparison, test |
| Class-File API (JEP 484) | 24 | Final | classfile/ClassFileExample, test |
| Flexible constructor bodies (JEP 513) | 25 | Final | constructors/SavingsAccount, test |
| Scoped values (JEP 506) | 25 | Final | scopedvalues/ScopedValueExample, test |
| Module import declarations (JEP 511) | 25 | Final | moduleimports/, test |
| Compact source files and instance main methods (JEP 512), multi-file source launcher (JEP 458) | 25 (22) | Final | Hello.java, multifile/, test; run with java src/main/source-launcher/Hello.java |
| Structured concurrency (JEP 505) | 25 | Preview (--enable-preview) |
test; also used in ScopedValueExample |
Features marked Preview require --enable-preview at compile time and at run time. Status is as of JDK 25. Java version (classic) means the example is not tied to a recent language change.
Requirements: JDK 25 and Maven 3.9+.
java -version # should report 25
mvn clean verify # compile and run all tests
mvn test -Dtest=ScopedValueExampleTests # run a single test classPreview features are already enabled in pom.xml: --enable-preview is passed to maven-compiler-plugin and to the Surefire argLine. To run compiled classes outside Maven, pass the flag yourself:
java --enable-preview -cp target/classes:target/test-classes <main-class>When adding a preview feature, keep it in a clearly named class or test and mark it Preview in the table above.