Java called method return value as a switch case value

case, java, method-call, switch-statement

Solution

As the error clearly states, `switch` only works with constant expressions.

You need to use an `if`.

Problem

I have to use a value, typically return via a method in switch case. ``` int getVal(){return 121;} switch(expr){ case getVal(): } ``` But its giving comilation error: constant expression required. I also tried like, ``` int _val = getVal(); switch(expr){ case _val: } ``` Having same result. Is there any workaround to implement it. Thanks, Amit

Original source