I know that 'switch' on strings was not workable before java 7 and was implemented only in java 7 and later. But I am using java 8 and I still get the error message "cannot switch on strings". Can anyone explain why and how do I get around it?
Asked
Active
Viewed 565 times
0
-
It should not be happened...maybe your selected JDK below 1.8 – Amit May 21 '18 at 05:11
-
@Amit : no I rechecked the version... it is jdk 1.8... – tech_logman May 21 '18 at 05:12
-
1Then you are not *really* using Java8. Go check your path, respectively the project setup in your IDE (you might have selected a Java8 JDK - but accidentally set the *compatibility mode* to Java 7 for example). If that is not the case, go create a [mcve]. – GhostCat May 21 '18 at 05:13
-
2@GhostCat :but even if the compatibility mode is set to java 7 , it should have worked.. isnt it ..coz java 7 has the ability – tech_logman May 21 '18 at 05:15
-
Why you don't put your code here? Please post your source code,,, – Amit May 21 '18 at 05:16
-
1@tech_logman True ;-) time for a coffee. So, check if compatbility mode is Java 6 ... – GhostCat May 21 '18 at 05:16
-
@tech_logman: Do you use a specific IDE? Eclipse? Intellij? .... – STaefi May 21 '18 at 05:17
-
@STaefi yes I am using eclipse helios – tech_logman May 21 '18 at 05:18
-
can you use newer version that helios? it might be eclipse issue. – Shanu Gupta May 21 '18 at 05:19
-
@Amit : it's just a single tester switch statement in the class. – tech_logman May 21 '18 at 05:19
-
@ShanuGupta : ok will try ... give me a moment , i already have oxygen installed.. – tech_logman May 21 '18 at 05:20
1 Answers
1
Even if you are using JDK 8
it's possible that you are not using the java 8 compiler compliance level .
If you are on eclipse, you can set the compiler level like this:

STaefi
- 4,297
- 1
- 25
- 43
-
no wait .. in helios compliance level is already set to '1.7' but i still get the error... wonder why? – tech_logman May 21 '18 at 05:31
-
Maybe you need to clean and Build again. Use project menu, select clean then select your project. Make sure `Build Automatically` is checked under `Project` menu. – STaefi May 21 '18 at 05:34
-
It's somehow wired. I can't really remember eclipse helios supports the JDK 8 or not. May be that's why it is not working. Maybe downloading a newer version of eclipse like Luna, Neon or Oxygen solve the problem. I'm on Luna and it's OK. – STaefi May 21 '18 at 05:47
-
To make sure that you are using the right JDK and path configurations on eclipse follow the steps I visually illustrated in the following answer: https://stackoverflow.com/questions/50245741/how-to-see-the-source-code-for-java-sdk-java-lang-java-util-classes-in-e/50245928#50245928 This answer is about setting JDK instead of JRE to the project to see the source codes of the JDK, but the steps should help you to see if you are using the right JDK on your project or not. – STaefi May 21 '18 at 05:56
-
true , the issue was resolved in Oxygen which i already have installed and compliance level was set to 1.8 . But it somehow does persist in helios even after the compliance level is set to 1.7 (rechecked for version and compliance). Also tried it on terminal with java version 8, and i get the result right. So i think it is an eclipse issue. – tech_logman May 21 '18 at 06:00