Why can't I declare static methods in an interface?

There are a few issues at play here. The first is the issue of declaring a static method without defining it. This is the difference between public interface Foo { public static int bar(); } and public interface Foo { public static int bar() { ... } } Java doesn't allow either, but it could allow the second.

The first is impossible for the reasons that Espo mentions: you don't know which implementing class is the correct definition Java could allow the latter, as long as it treated Interface s as first-class Object s. Ruby's Module s, which are approximately equivalent to Java's Interface s, allow exactly that: module Foo def self. Bar ... end end.

There are a few issues at play here. The first is the issue of declaring a static method without defining it. This is the difference between public interface Foo { public static int bar(); } and public interface Foo { public static int bar() { ... } } Java doesn't allow either, but it could allow the second.

The first is impossible for the reasons that Espo mentions: you don't know which implementing class is the correct definition. Java could allow the latter, as long as it treated Interfaces as first-class Objects. Ruby's Modules, which are approximately equivalent to Java's Interfaces, allow exactly that: module Foo def self.

Bar ... end end.

The reason why you can't have a static method in an interface lies in the way Java resolves static references. Java will not bother looking for an instance of a class when attempting to execute a static method. This is because static methods are not instance dependent and hence can be executed straight from the class file.

Given that all methods in an interface are abstract, the VM would have to look for a particular implementation of the interface in order to find the code behind the static method so that it could be executed. This then contradicts how static method resolution works and would introduce an inconsistency into the language.

This explanation doesn't explain the problem. Every interface have it's own class-file, it could contain the static-method. So no lookup for a particular implementation would be needed.

– Mnementh Sep 26 '08 at 8:38 Not every interface type in Java is within it's own file, nor should it be according to JLS. Additionally JLS doesn't stipulate that classes have to be always stored withing a file system, quite the contrary. – Vlad Gudim - AKA Totophil Feb 6 '09 at 12:22 @Totophil: Interfaces must not be in a single java-file, but it will have an own class-file after compiling.

That's what I wrote. – Mnementh Jan 7 at 16:00.

I'll answer your question with an example. Suppose we had a Math class with a static method add. You would call this method like so: Math.

Add(2, 3); If Math were an interface instead of a class, it could not have any defined functions. As such, saying something like Math. Add(2, 3) makes no sense.

Static methods are not instance methods. There's no instance context, therefore to implement it from the interface makes little sense.

There's a very nice and concise answer to your question here. (It struck me as such a nicely straightforward way of explaining it that I want to link it from here. ).

An interface is used for polymorphism, which applies to Objects, not types. Therefore (as already noted) it makes no sense to have an static interface member.

The reason lies in the design-principle, that java does not allow multiple inheritance. The problem with multiple inheritance can be illustrated by the following example: public class A { public method x() {...} } public class B { public method x() {...} } public class C extends A, B { ... } Now what happens if you call C.x()? Will be A.x() or B.x() executed?

Every language with multiple inheritance has to solve this problem. Interfaces allow in Java some sort of restricted multiple inheritance. To avoid the problem above, they are not allowed to have methods.

If we look at the same problem with interfaces and static methods: public interface A { public static method x() {...} } public interface B { public static method x() {...} } public class C implements A, B { ... } Same problem here, what happen if you call C.x()?

A explaining comment would be nice. – Mnementh Jan 7 at 16:01.

Perhaps a code example would help, I'm going to use C#, but you should be able to follow along. Lets pretend we have an interface called IPayable public interface IPayable { public Pay(double amount); } Now, we have two concrete classes that implement this interface: public class BusinessAccount : IPayable { public void Pay(double amount) { //Logic } } public class CustomerAccount : IPayable { public void Pay(double amount) { //Logic } } Now, lets pretend we have a collection of various accounts, to do this we will use a generic list of the type IPayable List accountsToPay = new List(); accountsToPay. Add(new CustomerAccount()); accountsToPay.

Add(new BusinessAccount()); Now, we want to pay $50.00 to all those accounts: foreach (IPayable account in accountsToPay) { account. Pay(50.00); } So now you see how interfaces are incredibly useful. They are used on instantiated objects only.

Not on static classes. If you had made pay static, when looping through the IPayable's in accountsToPay there would be no way to figure out if it should call pay on BusinessAcount or CustomerAccount.

It is possible to enforce static methods on a class by using annotations and the annotation processing tool though. I have written a blog entry about this. How to enforce static methods on a class by using annotations and the Annotation Processing Tool (APT).

Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. Be careful when posting copy and paste boilerplate/verbatim answers to multiple questions, these tend to be flagged as "spammy" by the community. – Kev?

Aug 13 at 12:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions