Preview Language Features in java

In this article i will be discussing the preview features in java

Bit of history

Java is now releasing a major version every six months. This was not the case before java 9. For example, java 7 was released in 2011, java 8 was released in 2014, and java 9 was released in 2017. You can see how much time has taken between major released earlier. This is changed for good now. They will be releasing major versions every six months which has a significant impact on the java ecosystem. This also introduced the concept called Preview Language Features in java

What is a preview feature?


With shorter release cycles, JDK team has introduced a concept called preview features. These are fully specified and implemented features in but impermanent. JDK team will collect feedback from the developers and evaluate the practical scenarios of the feature. Based on these facts, they will decide whether or not to keep this feature or to make changes. In other words, these are experimental features and safeguarded against accidentally used in production.

How to enable preview features?


To safeguard these features accidentally going into production , these features are not enabled by default. You will have to enable it manually by passing –enable-preview while both compiling and running stages. Let’s see how you can do this.
I have created a class called PreviewTest, and I will be using the switch expression feature, which will be released with JDK 13 as a preview feature.

public class PreviewTest {

	public static void main(String args[]) {
		printWordCount("MONDAY");
	}

	public static void printWordCount(String word) {
		switch (word) {
			case "MONDAY","TUESDAY", "SUNDAY" ->System.out.println(6);
			case "TUESDAY"                ->System.out.println(7);
			case "THURSDAY","SATURDAY"     ->System.out.println(8);
			case "WEDNESDAY"              ->System.out.println(9);
		}
	}
}

When you compile this without using –enable-preview flag, you can see it is complaining about using the preview feature.


In order to get rid of this error, we need to use the command

javac –enable-preview –release 13 PreviewTest.java.

To run, the command is java –enable-preview PreviewTest . This will successfully run and give you the expected output.

There are a couple of things to notice here.

  • Apart from using –enable-preview , there is another flag called —release which you must specify when compiling the source. –enable-preview cannot be used alone, it must be used with –release or –source. Otherwise, it will not compile. For –release you have to provide the release version which defines the feature
  • You have to provide –enable-preview both with compilation and run commands

Enabling preview features in intelij

Go to project settings and select the language level as Experimental features below.


Enabling preview in maven

In order to enable preview features in maven, you have to provide it as a compiler argument as below. Please make sure you have enabled it during both compilation and test execution 

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <release>13</release>
        <compilerArgs>
            --enable-preview
        </compilerArgs>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>--enable-preview</argLine>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <configuration>
        <argLine>--enable-preview</argLine>
    </configuration>
</plugin>


Leave a Reply

Your email address will not be published. Required fields are marked *