1

I have recently upgraded to java and I am trying my hand at the new default methods for interfaces. However I keep getting the syntax error on token "default", delete this token. Here is my build path where I would expect a problem to occur: enter image description here]![[1]

I suspect it has something to do with my build path but do not know what. In case it has something to do with my code here is the code I'm trying:

interface Test{
      default void sayHelloWorld() {
            System.out.println("Hello World");
        }

}

Anybody got any ideas?

Thijser
  • 2,625
  • 1
  • 36
  • 71

4 Answers4

7

You're using Java 5. Default methods are supported from Java 8 onwards. Update the JDK and it should work just fine.

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
  • I thought I did, when I look at the parts in the library they all say oracle java 8. – Thijser May 07 '14 at 08:57
  • If you look at the screenshot it clearly says "JRE Shared Library [J2SE-1.5]" which is Java 5. Go to the "Java Compiler" settings in your properties and you should be able to change the Java version. – blalasaadri May 07 '14 at 08:59
  • You mean under compliance it only goes up to 1.7? – Thijser May 07 '14 at 09:00
  • Well, that's certainly a problem. Try the Eclipse Preferences (Windows -> Preferences) and check Java -> Installed JREs. Is a correct Java 8 JDK installed? The compliance Level should be set to 1.8. – blalasaadri May 07 '14 at 09:03
  • java-8-oracle (default) of type standard VM. That sound correct? – Thijser May 07 '14 at 09:04
  • 1
    Sounds ok. Do you have a version of Eclipse that actually supports Java 8? This question may help to determine that: http://stackoverflow.com/questions/17619638/eclipse-kepler-java-8 – blalasaadri May 07 '14 at 09:06
2

Two things needs to be done.

  1. check for java build path and update to jdk1.8. Steps to configure:-

    • select properties from project.
    • select java build path --> Libraries -->select JRE System library(1.5).
    • make changes by selecting edit and update by pointing to latest jdk 1.8
  2. Ensure project compliance is set to jdk1.8, as the default methods are available only at source level 1.8 or above. Steps to configure:-

    • select properties from project.
    • select java compiler--> select jdk compliance and update all to jdk 1.8 from dropdown. Or make sure to select the "Use compliance from execution environment 'J2SE1.8' on java build path" (Needs to be done after the first step on build path)
Manoj
  • 121
  • 7
1

This helped me out :
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

sudo apt-get install oracle-java8-set-default
Check version:
java -version

Usman
  • 306
  • 1
  • 3
0

You're using J2SE-1.5 and to use "default" u need to upgrade Your java to java 8 ;)

Fincio
  • 198
  • 8