File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why an Interface cannot have static method?

 
Rama Lakshmi
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Anyone can tell the reasons for not to have static methods in
a interface.

Thanks and Regards
Rama
 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static mathods are class level method, meaning it belongs to the class it defined and implemented. But if we are allowed to have static methods in interfaces which class would the actual implementation(where we actually call the method) belongs to?
 
dee anderson
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All methods in an interface are purely abstract, they do not contain any body code. A static method can be called without making an instance of the class that contains it (class.staticmethod()).
 
Paul Somnath
Ranch Hand
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has been discussed at length before. Please read this thread.
 
Rama Lakshmi
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ThanQ Guys for a quick responce

Ragards
Rama
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other reason is

as a Rule

interface methods must be overridden, but on the other hand if you make your
method static in a interface that means you cannot override a method because
static methods cannot be overriden.

so you cannot add a static method in an interface
but you can put an object in an interface and access it with interface reference(like static way) like ;



public interface Bird
{
Wings one = new Wings();
}

public class Wings {

}

public class Test {

public static void main(String[] args) {

Wings w = Bird.s; // interface access

}

}

[ October 15, 2008: Message edited by: Anut Walidera ]
[ October 15, 2008: Message edited by: Anut Walidera ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic