Search code examples
javajep

How to test java 23 preview-features


I want to test JEP 468 (Derived Record Creation) but I'm unable to compile it.

In the JEP there is a paragraph:

This is a preview language feature, disabled by default

To try the examples in JDK 23 you must enable preview features:

  • Compile the program with javac --release 23 --enable-preview Main.java and run it with java --enable-preview Main; or,

  • When using the source code launcher, run the program with java --enable-preview Main.java; or,

  • When using jshell, start it with jshell --enable-preview.


  • So I installed jdk 23 (for windows > sha256: b18897bec6b1c6e0f639d95757eb0e3b0ec3d69720f6e4631874f2f9408075c5),

  • changed my PATH accordingly,

  • executed javac --version, which returned "javac 23"

  • wrote the following class using the derived record creation:

public class Main {

    record Point(int x, int y) {
        public Point {
            System.out.println("Point created at (" + x + ", " + y + ")");
        }
    }

    public static void main(String[] args) {
        var oldLoc = new Point(1, 2);
        Point nextLoc = oldLoc with {
            x = 0;
        };
        System.out.println("New point: " + nextLoc);
    }
}
  • executed the command javac --release 23 --enable-preview .\src\Main.java
  • and got the following compiler error:
.\src\Main.java:14: Fehler: ';' erwartet
        Point nextLoc = oldLoc with {
                              ^
.\src\Main.java:14: Fehler: Keine Anweisung
        Point nextLoc = oldLoc with {
                               ^
.\src\Main.java:14: Fehler: ';' erwartet
        Point nextLoc = oldLoc with {
                                   ^
3 Fehler

How can I get this to compile (and eventually run)?


Solution

  • As noted in the Comments above, the feature JEP 468: Derived Record Creation (Preview) is not included in Java 23.

    • The JEP’s status is candidate rather than Closed / Delivered.
    • The JEP is not listed as a feature on the Java 23 project page.

    So, keep an eye on Java 24.