0

suppose I have an interface:

public interface abcd{
    public int a();
    public void b();
    public void c();
    public String d(String h, String j);
}  

and I implement it in some class:

public class xyzw implements abcd{

}  

but I want the method d() to be static, but I can't do this:

public class xyzw implements abcd{
    public static void c(){
       //some code
    }
}

neither can I do this:

public interface abcd{
    public int a();
    public void b();
    public static void c();
    public String d(String h, String j);
}  

I wonder if there is something or some workaround or some language construct which allows me to make a method defined by an interface a static method?

juztcode
  • 1,196
  • 2
  • 21
  • 46
  • I don't think you can do that. Why don't create another method that is static instead? – Amongalen Jun 18 '20 at 09:20
  • Static methods cannot be overridden, called polymorphically, or implemented separately from their declaration. – khelwood Jun 18 '20 at 09:21
  • 3
    Does this answer your question? [Why can't I define a static method in a Java interface?](https://stackoverflow.com/questions/512877/why-cant-i-define-a-static-method-in-a-java-interface) – SAM Jun 18 '20 at 09:22
  • Check out: [Why can't I define a static method in a Java interface?](https://stackoverflow.com/questions/512877/why-cant-i-define-a-static-method-in-a-java-interface) – akuzminykh Jun 18 '20 at 09:23
  • 1
    @Steffi No, it does not because the question is *if there is something or some workaround or some language construct which allows me to make a method defined by an interface a static method*. – akuzminykh Jun 18 '20 at 09:24
  • How are you intending to call this method? If you are going to have an instance of your interface that you want to call the method on, then it doesn't need to be static; you can make it an instance method instead. If you don't have an instance, then it implementing an interface is irrelevant. – khelwood Jun 18 '20 at 09:26
  • This road leads to nothing good.. I can't think of any good reason to do this. It reminds me of the days that people would use interfaces to define constants and then "implement" them. Static methods are usually called statically anyway. So you'd call them as MyClass.method(). No good reason in Java to do this. – AminM Jun 18 '20 at 09:36
  • @khelwood , defining it this way would mean the method can be called without an instance and the class name would act as a namespace – juztcode Jun 18 '20 at 09:54
  • @khelwood , that is true, but the interface assures that there is a method to 1) read from memory 2) write to memory 3) read from database 4) write to database which is just cleaner to put all the stuff in once, and I can implement this interface to make sure these features are available to anything relating the database – juztcode Jun 18 '20 at 09:59
  • If you're going to refer to the class by name, you can just call the static method using the class. You don't need the static method to be declared in an interface to do that. – khelwood Jun 18 '20 at 10:01
  • @khelwood , and I wanted it static since I could change a few parameters to just get one information instead of having to instantiate the whole thing and a whole bunch of setup when I only needed one thing from the database – juztcode Jun 18 '20 at 10:01
  • @khelwood , like I had mentioned above, the inteface assures those 4 features for any class that needed to access the database – juztcode Jun 18 '20 at 10:02

4 Answers4

2

You can define a static method in interface, but only with implementation.

public interface A {
    public static void b() {
        System.out.println("Hi");
    }
}

Overriding of static methods is not allowed in Java, because you call it on Class object, not on implementation object.

DDovzhenko
  • 1,295
  • 1
  • 15
  • 34
1

If you can implement a static method in an interface, but you cannot overwrite it, remember that a static method is referenced by the class itself and not by an instance of it.

To solve your problem maybe you could define an abstract class

xxce10xx
  • 79
  • 4
0

No, its not possible and doesn't make any sense. An interface is meant to be implemented by subclasses you can only hava a non-abstract, implemented, static method in an interface. You could not statically call your interface method with

abcd.c()

when it has no implementation. Thats why static elements can not be overridden.

godsim
  • 181
  • 1
  • 9
0

It's not possible to override static methods in java.

However, in the subclass, you can declare static method with the same name and "mask it as" the original method - which is as close as you can get.

interface a {
    public static int f() {
        return 0;
    }
}
interface b extends a {
    public static int f() {
        return 1;
    }
}
System.out.println(a.f());
>> 0

System.out.println(b.f());
>> 1
Ecto
  • 1,125
  • 1
  • 7
  • 13
  • Which is a great way to create unreadable, ambiguous code and sooner or later will lead to wasting hours tracing weird bugs. If you intend to call a method on an object rather than on a class, it should not be static. –  Jun 18 '20 at 09:37