Java switch statement
Constant expression
Programming error
Java tutorial
Code debugging

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"
  • final local variables initialized with a literal or another constant expression
  • static final fields 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
  • final variables initialized from a method call (for example final int x = getValue())
  • static fields without final
  • 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

java
1public class StatusCodes {
2    static int OK = 200;          // not final
3    static int NOT_FOUND = 404;   // not final
4
5    public String describe(int code) {
6        switch (code) {
7            case OK:        // Compilation error: constant expression required
8                return "Success";
9            case NOT_FOUND: // Compilation error: constant expression required
10                return "Not found";
11            default:
12                return "Unknown";
13        }
14    }
15}

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:

java
1public class StatusCodes {
2    static final int OK = 200;
3    static final int NOT_FOUND = 404;
4
5    public String describe(int code) {
6        switch (code) {
7            case OK:
8                return "Success";
9            case NOT_FOUND:
10                return "Not found";
11            default:
12                return "Unknown";
13        }
14    }
15}

The same rule applies to local variables. A final local variable initialized with a literal is a compile-time constant:

java
1public void process(int status) {
2    final int TIMEOUT = 408;
3
4    switch (status) {
5        case TIMEOUT:
6            System.out.println("Request timed out");
7            break;
8    }
9}

But a final local variable initialized from a method call is NOT a compile-time constant:

java
1public void process(int status) {
2    final int timeout = getTimeoutCode(); // runtime value
3
4    switch (status) {
5        case timeout: // Compilation error: constant expression required
6            break;
7    }
8}

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:

java
1public enum HttpStatus {
2    OK(200),
3    NOT_FOUND(404),
4    INTERNAL_ERROR(500);
5
6    private final int code;
7
8    HttpStatus(int code) {
9        this.code = code;
10    }
11
12    public int getCode() {
13        return code;
14    }
15}
16
17// Usage in a switch
18public String describe(HttpStatus status) {
19    switch (status) {
20        case OK:
21            return "Success";
22        case NOT_FOUND:
23            return "Not found";
24        case INTERNAL_ERROR:
25            return "Server error";
26        default:
27            return "Unknown";
28    }
29}

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:

java
1switch (code) {
2    case 200:
3        return "Success";
4    case 404:
5        return "Not found";
6    default:
7        return "Unknown";
8}

This sacrifices readability for simplicity. Named constants are almost always preferable.

Which declarations compile as case labels

DeclarationCompiles as Case Label?Reason
case 42:YesLiteral is a constant expression
case 'A':YesCharacter literal is constant
case "hello":Yes (Java 7+)String literal is constant
final int X = 10; case X:YesFinal variable with literal initializer
static final int X = 10; case X:YesStatic final with literal initializer
static int X = 10; case X:NoMissing final
final int X = getValue(); case X:NoInitializer is not a constant expression
int X = 10; case X:NoVariable is not final

Switch expressions in Java 14+

Java 14 introduced switch expressions with arrow syntax. Case labels follow the same constant expression rules:

java
1static final int OK = 200;
2static final int NOT_FOUND = 404;
3
4String result = switch (code) {
5    case OK -> "Success";
6    case NOT_FOUND -> "Not found";
7    default -> "Unknown";
8};

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

TypeSupported Since
int, byte, short, charJava 1.0
Enum typesJava 5
StringJava 7
Any reference type, with pattern matchingJava 21 (preview in 17 to 20)
float, double, long, booleanNot 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.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms