Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can an interface define required static functions?

 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has also been discussed here
 
Kulbir Java
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic