57. Statement blocks

A label's arrow can point to a single statement (as in the examples from the previous two problems) or to a curly-braced block. This is pretty similar to the lambda blocks. Check out the following solution:

private static Player createPlayer(PlayerTypes playerType) {
return switch (playerType) {
case TENNIS -> {
System.out.println("Creating a TennisPlayer ...");
break new TennisPlayer();
}
case FOOTBALL -> {
System.out.println("Creating a FootballPlayer ...");
break new FootballPlayer();
}
case SNOOKER -> {
System.out.println("Creating a SnookerPlayer ...");
break new SnookerPlayer();
}
default ->
throw new IllegalArgumentException(
"Invalid player type: " + playerType);
};
}
Notice that we exit from a curly-braced block via break, not return. In other words, while we can return from inside a switch statement, we can't return from within an expression.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset