Java switch: 'constant expression required' when the value looks constant
Codemia Knowledge Hub ·
The "constant expression required" compiler error in a Java switch statement means the value in your case label is not a compile-time constant, even if it looks constant to you. The fix is to declare the variable as final with a literal initializer, use an enum, or use a literal value directly.
Why case labels must be compile-time constants
The Java Language Specification requires every case label in a switch statement to be a constant expression. A constant expression is a value the compiler can fully evaluate during compilation, not at runtime. This constraint exists because the compiler builds a jump table or lookup table from the case values, and it can only do that when every value is known before the program runs.
That is the source of the confusion. A variable can appear "constant" to a developer because it never changes at runtime, but the compiler does not perform that kind of analysis. It only recognizes specific syntactic patterns as compile-time constants.
What counts as a compile-time constant
The compiler treats these as constant expressions:
- Literal values:
42,'A',"hello" finallocal variables initialized with a literal or another constant expressionstatic finalfields initialized with a literal or constant expression- Constant expressions built from other constants using operators (for example
1 + 2)
These do NOT qualify:
- Non-final variables, even if never reassigned
finalvariables initialized from a method call (for examplefinal int x = getValue())staticfields withoutfinal- Values read from configuration, user input, or any runtime source
Enum constants are not constant expressions in the JLS sense, but a switch on an enum type accepts them as case labels by a separate rule, which is why Fix 2 works.
Code that triggers the error
The variables OK and NOT_FOUND are static but not final. The compiler cannot guarantee their values will not change before the switch executes, so it rejects them.
Fix 1: Add the final keyword
The most common fix is to declare the variables as static final:
The same rule applies to local variables. A final local variable initialized with a literal is a compile-time constant:
But a final local variable initialized from a method call is NOT a compile-time constant:
Fix 2: Use an enum
Enum case labels are checked at compile time without needing a constant expression, and enums are the idiomatic Java solution when you have a fixed set of related values:
Up to Java 20, the case labels in an enum switch must be the unqualified constant name (OK, not HttpStatus.OK); case HttpStatus.OK: fails with an enum switch case label must be the unqualified name of an enumeration constant. Java 21 (JEP 441) also accepts the qualified name.
Fix 3: Use literal values directly
When you do not want to extract constants, literal values always work:
This sacrifices readability for simplicity. Named constants are almost always preferable.
Which declarations compile as case labels
| Declaration | Compiles as Case Label? | Reason |
case 42: | Yes | Literal is a constant expression |
case 'A': | Yes | Character literal is constant |
case "hello": | Yes (Java 7+) | String literal is constant |
final int X = 10; case X: | Yes | Final variable with literal initializer |
static final int X = 10; case X: | Yes | Static final with literal initializer |
static int X = 10; case X: | No | Missing final |
final int X = getValue(); case X: | No | Initializer is not a constant expression |
int X = 10; case X: | No | Variable is not final |
Switch expressions in Java 14+
Java 14 introduced switch expressions with arrow syntax. Case labels follow the same constant expression rules:
Switch expressions also support pattern matching (Java 21+), but that does not change the constant expression requirement for traditional value-based cases.
Types you can switch on
| Type | Supported Since |
int, byte, short, char | Java 1.0 |
| Enum types | Java 5 |
String | Java 7 |
| Any reference type, with pattern matching | Java 21 (preview in 17 to 20) |
float, double, long, boolean | Not supported |
Other cases that still fail
Constants from another class without final. Importing a static field from a utility class does not make it a constant. The field must be static final with a constant initializer in the source class.
Interface fields look constant but might not be. Interface fields are implicitly public static final, so they do work as case labels, but only if initialized with a constant expression. If the initializer calls a method, it still fails.
Wrapper types cause confusion. You can switch on Integer, Character, Short or Byte: the value is unboxed, and the case labels must still be primitive constants such as 1 or 'a'. A null wrapper throws NullPointerException at the switch. Long is different, because long is not a switch type at all, so switch (someLong) with case 1L: does not compile.
Fall-through after a fix. Once the error is fixed, remember that Java switch statements fall through without break. Missing a break in one case silently executes subsequent cases.
Related reading
- Java Synchronized Block for .class
- Java synchronized method
- Java synchronized method lock on object, or method?
- Java system properties and environment variables
- java.lang.ClassCastException class java.lang.String cannot be cast to class [B (java.lang.String and [B are in module java.base of loader ''bootstrap
- java.lang.ClassCastExceptionxx cannot be cast to org.apache.avro.generic.IndexedRecord
- Java System.currentTimeMillis equivalent in C
- Java text classification problem
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.