This subject already has questions and answers, none with conclusive results. If this is not possible, then can someone determine the best workaround? Some of the answers say that this is possible with Java 8, but my IDE (eclipse) is still showing errors even though it is set to compiler compliance 1.8. If someone can get it to work on their system, let me know and I'll fix the problems with my system.
Problem: I need to create an interface with both static and non-static methods. An example of this:
// I need to create an interface with both static and non-static methods.
public interface TestInterface {
// No problem
int objectMethod();
// static is an illegal modifier in an interface
static int classMethod();
}
// TestClass is one of many classes that will implement the interface.
public class TestClass implements TestInterface {
// static and non-static variables are no problem
private int objectVal;
public static int classVal;
// this method has no problem
public int objectMethod() {
return objectVal;
}
// this one has problems because it implements a method that has problems
public static int classMethod() {
return classVal;
}
}
And I need a class that will use classes that implement the interface
// a class that will use classes that implement from the interface
// what is the syntax for this? my IDE doesnt like the implements keyword
public class TestGeneric <T implements TestInterface> {
// stored instances of the class
private T t;
public void foo() {
System.out.printf("t's value is: %d\n", t.objectMethod());
System.out.printf("T's value is: %d\n", T.classMethod());
}
}
The only problems in the above code are with the static methods. The only workaround I can think of is to not declare any methods as static, but use them in the way most similar to static use.