Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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)

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top