Call for feedback -- switch expressions in JDK 12

Remi Forax forax at
Fri Feb 28 10:17:26 UTC 2020

Hi Andy,

----- Mail original -----
> De: "Andy Fendley" <andy at>
> À: "jdk-dev" <jdk-dev at>
> Envoyé: Jeudi 27 Février 2020 09:36:48
> Objet: Re: Call for feedback -- switch expressions in JDK 12

> Hi Alex,
> Been using the new switch expressions in Java 12 and 13 for a bit and really
> like them. They cut code down code, get rid of break and the expression
> capability is useful. One issue I have, is that if I need more than one line of
> code for each case I have to enclose in a block which makes the code really
> ugly and worse to be honest. Using the old method this was not the case.

"Ugly" is a matter of taste.
Anyway, i think you have overlook the fact that we want to clean the scoping rules of switch.
Here is an example of a valid code with a switch using the colon (':') syntax

switch(kind) {
  case "car":
    var color = "red";
  case "bus":
    color = "yellow";

as you can see, it's perfectly legal to declare a variable in one case and reuse it in another one.
Given that the issue is about local variable scoping, the easiest way to fix the problem is to introduce curly braces.

Here is the same code that does not compile using the arrow ('->') syntax
switch(kind) {
  case "car" -> {
    var color = "red";
  case "bus" -> {
    color = "yellow";   // here color is unknown

So it's no very much a matter of ugliness and more a matter of fixing the existing issues of switch.
The new syntax fix the fallthroughs and also the weird scoping rule.

> Hope this helps.
> Andy


More information about the jdk-dev mailing list