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?

12
  • I don't think you can do that. Why don't create another method that is static instead? Commented Jun 18, 2020 at 9:20
  • Static methods cannot be overridden, called polymorphically, or implemented separately from their declaration. Commented Jun 18, 2020 at 9:21
  • 3
    Does this answer your question? Why can't I define a static method in a Java interface? Commented Jun 18, 2020 at 9:22
  • Check out: Why can't I define a static method in a Java interface? Commented Jun 18, 2020 at 9: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. Commented Jun 18, 2020 at 9:24

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.