Issue
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?
Solution
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;
}
}
}
Answered By - burglarhobbit
Answer Checked By - Mildred Charles (JavaFixing Admin)