3

So can you do something like this in Java:

Can you get the value being switched on inside a switch expression

I have quite a few cases in my code which look like this (actual logic code removed for clarity reasons):

    switch (weatherSystem.getRealClass().getSimpleName())
    {
        case "SyncWeatherSystem":
            logger.info("initializing sync weather system");
            …
            break;
        case "AsyncWeatherSystem":
            logger.info("initializing  async weather system");
            …       
            break;
        case "FixedWeatherSystem":
            logger.info("initializing fixed weather system");
            …
            break;
        case "NoWeatherSystem":
            logger.info("initializing no weather system");
            …
            break;
    }

And I really would love to do like:

    switch (weatherSystem.getRealClass().getSimpleName())
    {
        case "SyncWeatherSystem":
            logger.info("initializing {}", case.value);
            …
            break;
        case "AsyncWeatherSystem":
            logger.info("initializing {}", case.value); 
            …
            break;
        case "FixedWeatherSystem":
            logger.info("initializing {}", case.value);
            …
            break;
        case "NoWeatherSystem":
            logger.info("initializing {}", case.value);
            …
            break;
    }

Is this possible in Java?

4
  • 1
    You don't need the switch at all - just use string concatenation "directly." Commented Mar 19, 2022 at 19:22
  • @EJoshuaS-StandwithUkraine Only if default isn't a no-op. Commented Mar 19, 2022 at 19:23
  • @ElliottFrisch True. Commented Mar 19, 2022 at 19:24
  • Why do you need an alternative way to get the value, instead of using the same expression as in the switch parentheses? If it takes time to generate and return the value, store it in a variable (perhaps a final one) before using it in a switch. Commented Mar 19, 2022 at 19:40

1 Answer 1

4

No. It is not. But, weatherSystem.getRealClass().getSimpleName() is. I suggest you save that value to a local variable. And all your case(s) seem to do the same thing. So, as posted, you could simplify it. Like

String sName = weatherSystem.getRealClass().getSimpleName();
switch (sName)
{
    case "SyncWeatherSystem":
    case "AsyncWeatherSystem":
    case "FixedWeatherSystem":
    case "NoWeatherSystem":
        logger.info("initializing {}", sName);
        break;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Good Answer. I suggest being more explicit about the main change being the move of the call to getSimpleName to a named local variable. I had to look a few times to see what you did. Perhaps you should just show that first, before separately showing the collapsed case cascade.
I am sorry - the actual logic code is actually removed, there is code between logger.info and break;
@Snorik In the future, I suggest you show additional but irrelevant code by adding a line with a single character, ellipsis .
@BasilBourque thank you for the reminder. Just added [...] to make it clear
@Snorik I altered your edit to use the actual ellipsis character ( Option + Semicolon on macOS), and to move indented with your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.