Quick revision
The short answer
A preview feature is a finished language or JVM feature, such as a new kind of pattern, that is not yet permanent; we enable it with --enable-preview together with --release N (or --source N) when compiling and --enable-preview when running, and the class files then run only on that exact JDK version. An incubator module is a new API shipped in a jdk.incubator.* module, which we enable with --add-modules. Both can change or disappear in later releases.
What to remember
In this answer, we saw that preview features are complete language or VM features that are off by default, enabled with --enable-preview plus --release or --source at compile time and --enable-preview at run time, and that their class files run only on the exact JDK that compiled them. Incubator modules are APIs in jdk.incubator modules, enabled with --add-modules. Both can change or be removed, and the withdrawal of string templates after Java 21 and 22 shows that this is a real possibility.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A preview feature is a finished language or JVM feature, such as a new kind of pattern, that is not yet permanent; we enable it with --enable-preview together with --release N (or --source N) when compiling and --enable-preview when running, and the class files then run only on that exact JDK version. An incubator module is a new API shipped in a jdk.incubator.* module, which we enable with --add-modules. Both can change or disappear in later releases.
Why this question is asked
Every Java release now ships some features that are not yet final, and the flags to try them are easy to get wrong. Interviewers ask this to check that we know the difference between trying a feature and depending on it, which matters as soon as someone wants to put new syntax into a production service. In this answer, we will recap why these two mechanisms exist, see how to enable a preview feature and what happens to the class files, then do the same for an incubator module, and finish with the String templates story, which shows why previews are not promises.
1Why the JDK Ships Unfinished Features
Since Java 10, a new JDK feature release arrives every six months. That leaves little time to get a big feature right on the first try, and once something becomes part of the Java language or the standard library, it is almost impossible to remove. So the JDK has two ways to put new work into developers' hands while it can still change.
A preview feature is defined by JEP 12. It applies to the language and the virtual machine: new syntax, new kinds of patterns, new class file rules. The feature is fully specified and implemented, not a prototype, but it is switched off by default and may change before it becomes final, usually after one or more rounds of preview.
An incubator module is defined by JEP 11. It applies to APIs, the classes and methods we call. The new API lives in a module whose name starts with jdk.incubator, such as jdk.incubator.vector for the Vector API, so it cannot be confused with the permanent java.* packages.
2Enabling a Preview Feature
Let us say Java 25 had a preview language feature we wanted to try. Compiling code that uses it without any flag fails with an error telling us the feature is a preview feature and disabled by default. To compile it, we tell javac which release we target and that we accept preview features.
Notice that javac requires --release or --source with --enable-preview. The reason is that a preview feature belongs to one specific release; javac must know which release's preview features we mean. In Maven we pass the flag through the compiler plugin's compilerArgs and through the Surefire plugin's argLine for tests, and in Gradle through options.compilerArgs and jvmArgs for test and run tasks.
The run-time flag matters just as much. The JVM refuses to load a class that uses preview features unless --enable-preview is on, so nobody runs preview code by accident.
3Why Preview Class Files Only Run on One JDK
When javac compiles with preview features, it marks the class file in a special way: the minor version number becomes 65535. For Java 25 the class file says major version 69, minor 65535. The JVM accepts such a file only if its own version is exactly 25 and --enable-preview is on.
This means a JAR built with preview features on 25 will not run on 26, even with --enable-preview. The reason is that the feature may have changed between the two releases, and the JVM cannot know whether the old class file matches the new rules. So code that uses previews must be recompiled, and possibly rewritten, at every JDK upgrade.
| Question | Preview feature | Incubator module |
|---|---|---|
| What it covers | Language syntax and JVM behaviour | A new API (classes and methods) |
| Where it lives | Inside javac and the JVM | A jdk.incubator.* module |
| Compile flag | --enable-preview with --release N | --add-modules jdk.incubator.x |
| Run flag | --enable-preview | --add-modules jdk.incubator.x |
| Class files tied to one JDK? | Yes, minor version 65535 | No, but the API may change |
| Can it change or vanish? | Yes | Yes |
4Enabling an Incubator Module
An incubator module is a normal module that the JDK does not resolve by default. To use it, we add it to the module graph with --add-modules at compile time and at run time. The Vector API, which has been incubating for many releases, is the usual example.
So the run prints a warning on every start, which is the JDK's way of reminding us that we depend on something unfinished. If our code is itself a module, we can instead write requires jdk.incubator.vector in module-info.java. Unlike preview class files, these class files are ordinary, but the next JDK may rename or change the API, so the code may still fail to compile or link after an upgrade.
5String Templates: Proof That Previews Are Not Promises
String templates let us write STR."Hello \{name}" to build strings with embedded expressions. They were previewed in Java 21 and again in Java 22. During the second preview, the feedback and design discussion showed problems with the approach, and the feature was withdrawn: Java 23 shipped without it, and code that used it no longer compiled.
This is the strongest argument for treating previews as experiments. A team that had built production code on string templates would have had to rewrite every use before upgrading to 23. Most previews do become final, often with small changes, as records, sealed classes and pattern matching for switch did, but the JDK keeps the right to change or drop them, and it used that right.
6Follow-up questions
Can a library published to Maven Central use preview features?
It can, but it should not. Its class files would only load on one JDK version with --enable-preview, so every user would be forced onto that exact version and flag. Libraries should only use final features, or ship a separate experimental artifact.
Is an experimental feature the same as a preview?
No. Experimental features, usually VM options unlocked with -XX:+UnlockExperimentalVMOptions, are early implementations in HotSpot that are not considered ready. Preview features are complete and specified; experimental ones may not be. Each JEP says which category applies.
How do we enable preview features in tests with Maven?
We add --enable-preview to the maven-compiler-plugin compilerArgs, set the release, and add --enable-preview to Surefire's argLine. Forgetting the Surefire part is common: the code compiles but every test fails with the preview-not-enabled error.
7Conclusion
In this answer, we saw that preview features are complete language or VM features that are off by default, enabled with --enable-preview plus --release or --source at compile time and --enable-preview at run time, and that their class files run only on the exact JDK that compiled them. Incubator modules are APIs in jdk.incubator modules, enabled with --add-modules. Both can change or be removed, and the withdrawal of string templates after Java 21 and 22 shows that this is a real possibility.
Open with
A preview feature is a language or JVM feature that is fully built but not yet permanent, like pattern matching was before it became final. To use one I compile with --enable-preview plus --release N or --source N, because previews belong to one specific release, and I also run with --enable-preview, otherwise the JVM refuses to load the class.
Then, if they want more
The class file gets minor version 65535, which means it only runs on that exact JDK version, so every upgrade needs a recompile and maybe a rewrite. An incubator module is different: it is a new API rather than syntax, shipped in a module named jdk.incubator something, like jdk.incubator.vector.
I enable it with --add-modules at compile and run time, and the JVM prints a warning that I am using incubator modules. Both kinds can change or be removed. String templates are the example I remember: they were previewed in 21 and 22 and then withdrawn in 23, so any code using them simply stopped compiling.
That is why I treat previews as something to experiment with and give feedback on, not something to build production code on.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 21, Java 10, Java 22
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the difference between a preview feature and an incubator module in the JDK? How...": https://scrivio-interview-library.netlify.app/java/java-versions/1788e581ab/