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.


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);
    }
}
.\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.

    So, keep an eye on Java 24.