10

According to The Java Tutorials, in Java SE 7 and later, you can use a String object in the switch statement's expression.

String s = ...
switch(s){
    //do stuff
}

But is this true? I've installed the JRE and added it to the build path of my Eclipse project, but I'm getting the following compile-time error:

Cannot switch on a value of type String. Only convertible int values or enum constants are permitted

Also, I think I've got it configured correctly since I was able to use its java.nio.file.Files class, as well as JLayer.

Any ideas?

4
  • 5
    Do you have a recent, Java 7 enabled Eclipse IDE? Which version are you using? Eclipse brings its own Java Compiler!
    – Daniel
    Jun 3, 2011 at 19:45
  • 1
    Yeah try to compile it outside of Eclipse and see if it works.
    – user686605
    Jun 3, 2011 at 19:46
  • @Daniel, I guess I'm naive when it comes to IDEs. I thought configuring the build path with the appropriate JRE would set things straight, seeing that jdk7-specific features are compiling and running using Eclipse. I guess I was just caught off-guard when this feature didn't compile/run as well. I hope you weren't being sarcastic...
    – mre
    Jun 3, 2011 at 20:08
  • 1
    Since Eclipse supports partial compiling of classes, and creates valid class files even if single functions contain compile errors, it simply needs its own compiler. In addition, Eclipses compiler supports incremental compilations, etc. Just use a recent eclipse version and you are done.
    – Daniel
    Jun 4, 2011 at 13:47

3 Answers 3

12

While it is true that the JDT team has implemented the Switch on String feature, the support for Java 7 won't be before Eclipse 3.7.1:

See bug 288548:

Due to late availability of JSR-292 (Invoke Dynamic) and JSR-334 (Project Coin) and due to the official release date (July 28, 2011) of Java 7 being after 3.7 ships we had to defer the Java 7 support to 3.7.1. It has not yet been decided whether this will be available as part of the 3.7.1 downloads or as separate feature update.

The work for the Java 7 features is currently in progress in the 'BETA_JAVA7' branch and we will deliver separate updates for the stable builds in order to provide early access to the Java 7 features for interested parties.

3
1

Eclipse doesn't support yet compilation on JDK 7, for try the new feature you need to use NetBeans 7 or compile with standard javac compiler in the bundle of the JDK 7 by hand or with the help of an ant script.

0

Use Eclipse 3.8 (first milestone came some days ago). Don't forget to set the compiler settings to Java 7.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.