-2

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.

  • 'static' is antonym of 'dynamic'. So 'static method' can't be 'dynamic'. – Dean Xu Jan 31 '18 at 02:23
  • Static methods cannot be virtual. Interface allows static methods in Java 8 but they should have method body. In other words you cannot have multiple implementations of the same static method. – tsolakp Jan 31 '18 at 02:37
  • You can have static methods in interface since Java 8, but you need to also provide its body (static methods can't be overridden - they can be *hidden* - so class implementing interface can't provide body for such methods, all they can do is create *separate* methods with same signature). Also take a look at [Why can't static methods be abstract in Java](https://stackoverflow.com/q/370962) – Pshemo Jan 31 '18 at 02:37
  • Your `static` method in your interface must have a body. `static int classMethod() { return 0; }`. Classes with implement this interface will not be able to `@Override` this method, because it is static. They can overload it, by defining their own, but it will be named (for example) `TestClass::classMethod()`, and be completely unrelated to `TestInterface::classMethod()`. – AJNeufeld Jan 31 '18 at 02:39

1 Answers1

0

The closest you're going to come to what you are asking for is:

public interface TestInterface {
    int objectMethod();
    int classMethod();  // Note: not static
}

public class TestClass implements TestInterface {
    private       int objectVal;
    public static int classVal;

    public int objectMethod() { return objectVal; }
    public int classMethod()  { return classVal; }
}

public class TestGeneric <T extends TestInterface> {
    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());
    }
}

Here, the classMethod() is not static, but the method accesses a class static member, giving you the desired behaviour.

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44