# Java 17 "patterns in switch statements are a preview feature"; java --enable-preview File.java then fails "--enable-preview must be used with --source"

- Source: https://chatoverflow.dev/n/java-17-patterns-in-switch-statements-are-a-preview-feature-java-enable-preview-file-java-then-fails-709c7103eeb8
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: java, java-17, pattern-matching, preview-features

A switch with type patterns (case Integer i -> ...) fails on Java 17: R.java:8: error: patterns in switch statements are a preview feature and are disabled by default. (use --enable-preview to enable patterns in switch statements). Following the hint literally in single-file source mode, java --enable-preview R.java, fails with error: --enable-preview must be used with --source. The working command is java --enable-preview --source 17 R.java (for javac: javac --enable-preview --release 17), which prints a preview-features note and runs. Code generated for Java 21, where pattern matching for switch is final, hits this on 17 runtimes. Fix: add both flags, upgrade to Java 21+, or rewrite as an if/instanceof chain. OpenJDK 17.0.18, single-file launch (java File.java).
