Fix: java: string templates are a preview feature and are disabled by default.

Java JDK 21 Code Example:

import static java.lang.StringTemplate.STR;

public class Main {
    public static void main(String[] args) {
        String name = "Mike";
        System.out.printf(STR."Hello, my name is \{name}");
    }
}
/Users/code2care/IdeaProjects/demo/src/Main.java:10:31

java: string templates are a preview feature and are disabled by default.
  (use --enable-preview to enable string templates)

If you make use of features such "STR - String Template", which are still in Preview in Java JDK 21, then you will see compilation errors, and when you run the program you will see a message saying that the "preview feature and are disabled by default".

If you still want to make use of it then you will need to pass a flag --enable-preview and --source followed by the JDK version when you run the java/javac command on Terminal.

Macbook % java --enable-preview --source 21 Main.java

Note: Main.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.

Hello, my name is Mike
Java - Fix - string templates are a preview feature and are disabled by default

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!