HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Switch on an enum

What javac does

Switch on an enum

A lookup table in a synthetic class, so reordering the enum cannot break the switch.

A switch on an enum looks like a switch on ordinal(), but when the enum is compiled separately its ordinals could change: reorder the constants, recompile only the enum, and every switch would jump to the wrong case. So for java.time.DayOfWeek, javac creates a synthetic class, EnumSwitch$1, holding an int array named $SwitchMap$java$time$DayOfWeek. Its static initialiser fills the array at run time, mapping each case's constant (by its current ordinal) to 1, 2, …, and the switch is on that number.

The source

// A switch on an enum. For an enum compiled separately, javac switches on a table that a synthetic
// class fills in when it first runs; for an enum in the same file, it can switch on ordinal().
import java.time.DayOfWeek;

public class EnumSwitch {
    enum Size { SMALL, MEDIUM, LARGE }

    static int price(Size size) {
        switch (size) {
            case SMALL:
                return 3;
            case MEDIUM:
                return 4;
            default:
                return 5;
        }
    }

    static boolean weekend(DayOfWeek day) {
        switch (day) {
            case SATURDAY:
            case SUNDAY:
                return true;
            default:
                return false;
        }
    }
}

Compiled by javac 25 with -g and --release 8 to 25. Where releases produce different bytecode, each version is shown.

What to look for

  • weekend reads the array with getstatic, indexes it with day.ordinal() and iaload, and switches on the result.
  • Every store into the map is wrapped in a handler for NoSuchFieldError, so if a constant is later removed from the enum, the class still initialises; that case just never matches.
  • Size is declared in the same file, so it is always compiled with the switch. javac 25 switches on its ordinal() directly, with no map.

EnumSwitch.class

static int price(EnumSwitch$Size size)

javac --release 8 to 25 (the same bytecode for every release)

9switch (size) {
0aload_0 // size
1invokevirtual #7// EnumSwitch$Size.ordinal() : int
4lookupswitch { 0→32, 1→34, default→36 }
11return 3;
32iconst_3
33ireturn
13return 4;
34iconst_4
35ireturn
15return 5;
36iconst_5
37ireturn

Open in the viewer

static boolean weekend(DayOfWeek day)

javac --release 8 to 25 (the same bytecode for every release)

20switch (day) {
0getstatic #13// EnumSwitch$1.$SwitchMap$java$time$DayOfWeek : int[]
3aload_0 // day
4invokevirtual #19// DayOfWeek.ordinal() : int
7iaload
8lookupswitch { 1→36, 2→36, default→38 }
23return true;
36iconst_1
37ireturn
25return false;
38iconst_0
39ireturn

Open in the viewer

EnumSwitch$1.class

A separate class file, EnumSwitch$1.class, that javac generated from the same source.

static { … }

javac --release 8 to 25 (the same bytecode for every release)

20switch (day) {
0invokestatic #1// DayOfWeek.values() : DayOfWeek[]
3arraylength
4newarray int
6putstatic #7// EnumSwitch$1.$SwitchMap$java$time$DayOfWeek : int[]
9getstatic #7// EnumSwitch$1.$SwitchMap$java$time$DayOfWeek : int[]
12getstatic #13// DayOfWeek.SATURDAY : DayOfWeek
15invokevirtual #17// DayOfWeek.ordinal() : int
18iconst_1
19iastore
20goto 24
23astore_0 catch NoSuchFieldError
24getstatic #7// EnumSwitch$1.$SwitchMap$java$time$DayOfWeek : int[]
27getstatic #23// DayOfWeek.SUNDAY : DayOfWeek
30invokevirtual #17// DayOfWeek.ordinal() : int
33iconst_2
34iastore
35goto 39
38astore_0 catch NoSuchFieldError
39return
FromToHandlerCatches
92023java.lang.NoSuchFieldError
243538java.lang.NoSuchFieldError

Open in the viewer

← Switch on a StringAll topicsAutoboxing →

By the author of this site

Reading bytecode is half the story; running it is the other half. You execute exactly this kind of code in Build a JVM in Rust: Class files, an interpreter, objects and invokedynamic from scratch, on the real JDK class library.

Read Chapter 1 free

Java and the JVM are trademarks of Oracle; this site is not affiliated with Oracle. Field names follow the Java Virtual Machine Specification; the explanations are our own.

Sister sites: What's in my DB · LLM Anatomy · Distribution Atlas · Notation Decoder · uresk.net

Made by Spencer Uresk · About · Files you open stay on your device.