1

I have this code:

public interface Type {
    public static Type match(String string) {
        try {
            return TypeBuiltIn.valueOf(string.toUpperCase());
        } catch (Exception e) {
            return null;
        }
    }
}

I was doing the same thing as some guy in the tutorial, for him it works fine, but I get an error on match(String string):

Illegal modifier for the interface method match; only public & abstract are permitted

I tried to remove the static, but nothing works. It says I should remove the method body, but what do I do then?

6
  • 2
    What Java version are you using?
    – Maroun
    Aug 30, 2015 at 11:17
  • 1
    I do not know much of Java, but maybe you should change the type to something else other than public because you are using return?
    – user41805
    Aug 30, 2015 at 11:18
  • 2
    Make sure you are compiling with Java 8.
    – Tunaki
    Aug 30, 2015 at 11:18
  • 5
    interface methods marked static haven't always been allowed. Compile with Java 8.
    – bcsb1001
    Aug 30, 2015 at 11:23
  • 1
    Also, you shouldn't catch just Exception, that's never good practice. I think you need to catch EnumConstantNotFound in your case.
    – Marco
    Aug 30, 2015 at 12:07

3 Answers 3

4

If you are using a Java version below Java 8, this code would not work because interface does not support static methods for java versions below Java 8. You need to update your Java version from this link, and edit your environment variable path from your system settings.

If you don't intend to update your java version then your Interface would not support any static method. You have to implement interfacename for the class and have the particular body inside the static method within a class.

For that, your interface should look as below:

public interface Type {
    public abstract Type match(String string);
}

And you class should be as below:

public class YourDesiredClassname implements Type {

    public static Type match(String string) {
        try {
            return TypeBuiltIn.valueOf(string.toUpperCase());
        } 
        catch (Exception e) {
            return null;
        }
    }   
}
1
  • I already have Java 1.8.0_60, all system variables are ok, but it still doesn't work... There's the same error.
    – 2xxx2
    Aug 30, 2015 at 14:24
1

please see the output of command java -version. The first line it prints should be something like

java version "1.8.xxxx"

Static methods default implementation is not allowed before java 8.

3
  • I have java 1.8.0_60 installed, but when I go to Window -> Preperences -> Java -> Compiler, the Compiler compliance levels are from 1.3 to 1.7, I can't choose 1.8! :O
    – 2xxx2
    Aug 30, 2015 at 14:22
  • 1
    @2xxx2 That's not how you edit your Java compiler. Do as I say. Goto Control Panel -> System -> Advanced System Settings -> Environment Variables... -> Create New Variable for User. Variable name = "Path" (without quotation) Variable value = "path of your bin folder where java jdk 1.8.0_60 is installed" (again, without quotation) Aug 30, 2015 at 17:02
  • I know, it was set to the 1.8.0_60's bin folder but it didn't work. But it's ok now, I fixed it. Thanks anyway. :P
    – 2xxx2
    Aug 30, 2015 at 18:23
1

Wow... You guys are amazing! Thank you all so much for the help, I tought I would get one answer in about a week, but I got 7 in a few hours. I was using Kepler so it didnt work, but then I installed Java 8 Support and it works.

If someone has the same problem, here's the page for Java 8 Support: https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler

Thanks everyone for helping. :)

1
  • greg-449 said that only Luna and Mars support Java 8 so I googled a fix for Kepler and that's where I found it... Thanks, Greg! :P
    – 2xxx2
    Aug 30, 2015 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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