56. Multiple case labels

Before JDK 12, a switch statement allowed a single label per case. Starting with the switch expressions, a case can have multiple labels separated by a comma. Check out the following method that exemplifies multiple case labels:

private static SportType 
fetchSportTypeByPlayerType(PlayerTypes playerType) {

return switch (playerType) {
case TENNIS, GOLF, SNOOKER ->
new Individual();
case FOOTBALL, VOLLEY ->
new Team();
};
}

So, if we pass to this method TENNIS, GOLF, or SNOOKER, it will return an instance of the Individual class. If we pass FOOTBALL or VOLLEY, it will return an instance of the Team class.

..................Content has been hidden....................

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