Final variable initialization problem with exhaustive switch

Mateusz Romanowski romanowski.mateusz at gmail.com
Wed Jan 12 21:22:42 UTC 2022


Hi Dimitris et al.,
I believe there is confusion on whether we are talking about switch
statement or switch expression.

If I assign value of this `switch` to a variable, it compiles:
```
        final int a;
        Object ignored = switch (p) {
            case ODD -> a = 1;
            case EVEN -> a = 0;
        };
        int b = a + 1;
```

Otherwise, "error: variable a might not have been initialized" is shown.

Cheers,
Mateusz


On Wed, Jan 12, 2022 at 10:19 PM Pedro Lamarão <pedro.lamarao at prodist.com.br>
wrote:

> This is what I got:
>
> PS E:\> E:\opt\jdk-17+35\bin\java --version
> openjdk 17 2021-09-14
> OpenJDK Runtime Environment Temurin-17+35 (build 17+35)
> OpenJDK 64-Bit Server VM Temurin-17+35 (build 17+35, mixed mode, sharing)
> PS E:\> E:\opt\jdk-17+35\bin\javac --release 17 --enable-preview Main.java
> Main.java:11: error: variable a might not have been initialized
>         int b = a + 1;
>                 ^
> 1 error
>
> Regards,
> Pedro.
>


More information about the amber-dev mailing list