A preview feature is:
- a new feature of the Java programming language ("preview language feature"), or
- a new module, package, class, interface, field, method, constructor, or enum constant in the
java.*orjavax.*namespace ("preview API")
that is fully specified, fully implemented, and yet impermanent. It is available in implementations of a given release of the Java SE Platform to provoke developer feedback based on real world use; this may lead to it becoming permanent in a future release of the Java SE Platform.
Implementations must disable, at both compile time and run time, the preview features defined by a given release of the Java SE Platform, unless the user indicates via the host system, at both compile time and run time, that preview features are to be enabled.
The preview features defined by a given release of the Java SE Platform are enumerated in the Java SE Platform Specification for that release. The preview features are specified as follows:
-
Preview language features are specified in standalone documents that indicate changes ("diffs") to The Java® Language Specification for that release. The specifications of preview language features are incorporated into The Java® Language Specification by reference, and made a part thereof, if and only if preview features are enabled at compile time.
Java SE 19 defines two preview language features: Pattern Matching for
switchand Record Patterns. The standalone documents which specify these preview features are available at the Oracle web site which hosts The Java® Language Specification:https://docs.oracle.com/javase/specs/. -
Preview APIs are specified within the Java SE API Specification for that release.
The rules for use of preview language features are as follows:
-
If preview features are disabled, then any source code reference to a preview language feature, or to a class or interface declared using a preview language feature, causes a compile-time error.
-
If preview features are enabled, then any source code reference to a class or interface declared using a preview language feature causes a preview warning, unless one of the following is true:
- The reference appears in a declaration that is annotated to suppress preview warnings (§9.6.4.5).
- The reference appears in an import declaration (§7.5).
When preview features are enabled, Java compilers are strongly encouraged to give a non-suppressible warning for every source code reference to a preview language feature. Details of this warning are beyond the scope of The Java® Language Specification, but the intent should be to alert programmers to the possibility of code being affected by future changes to preview language features.
Some preview APIs are described as reflective by the Java SE Platform Specification, principally in the java.lang.reflect, java.lang.invoke, and javax.lang.model packages. The rule for use of reflective preview APIs is as follows:
-
Whether preview features are enabled or disabled, any source code reference to a reflective preview API element causes a preview warning, unless one of the following is true:
- The declaration where the reference appears is within the same module as the declaration of the reflective preview API element.
- The reference appears in a declaration that is annotated to suppress preview warnings.
- The reference appears in an import declaration.
All preview APIs not described as reflective in the Java SE Platform Specification are normal. The rules for use of normal preview APIs are as follows:
-
If preview features are disabled, then any source code reference to a normal preview API element causes a compile-time error, unless:
- The declaration where the reference appears is within the same module as the declaration of the normal preview API element.
-
If preview features are enabled, then any source code reference to a normal preview API element causes a preview warning, unless one of the following is true:
- The declaration where the reference appears is within the same module as the declaration of the normal preview API element.
- The reference appears in a declaration that is annotated to suppress preview warnings.
- The reference appears in an import declaration.