wood burning stoves 2.0*
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
Author

Can an interface define required static functions?

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

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?

Thanks
Pat
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: 2969
    
    9
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: 784

@ 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


http://muhammadkhojaye.blogspot.com/
Kulbir Java
Greenhorn

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.


Regards
Kulbir
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can an interface define required static functions?
 
Similar Threads
Brain Teasers
No Banner for JSF book promo?
May 31, 2001 - test Job Offer $xxx,xxx Colorado Springs
Idle's mind is a devils workshop
Testing