File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Can an interface define required static functions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can an interface define required static functions?" Watch "Can an interface define required static functions?" New topic

Can an interface define required static functions?

Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

I think the answer is no, or at least I've never seen it. But I thought I'd ask the collective wisdom of the Ranchers.

Can it? If so, how?

Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Interface cannot contain static functions.
Could contain only static fields (fields in interface is always implicitly 'public static final', even if these modifiers are omitted).
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
Since you're asking for "collective wisdom", I'll confirm that Ireneusz is completely correct here - interfaces can never declare static methods. There's no way to do this in Java. The closest options I can think of are to either refactor to make the methods non-static, or add some reflection-based code that checks at runtime to see if a given class implements the methods you require. I don't see a way to do better.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

@ Pat

See Method Declarations

Following is quote from JLS3.0

Note that a method declared in an interface must not be declared static, or a compile-time error occurs, because static methods cannot be abstract.

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

This has also been discussed here
Kulbir Java

Joined: Feb 17, 2010
Posts: 5
Yes. I do support the above comments

Inteface methods are implicilty public and abstract and abstract methods can not be declared as static.

And the mthods should belongs to the object not the class and the static methods does not belongs to object.

I agree. Here's the link:
subject: Can an interface define required static functions?
It's not a secret anymore!