<p>The topic says the most of it - what is the reason for the fact that static methods can't be declared in an interface?</p>

<pre><code>public interface ITest {
    public static String test();
}
</code></pre>

<p>The code above gives me the following error (in Eclipse, at least): "Illegal modifier for the interface method ITest.test(); only public &amp; abstract are permitted".</p>

[{'answer_id': 21819, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 3, 'selected': False, 'text': "<p>Static methods are not instance methods. There's no instance context, therefore to implement it from the interface makes little sense.</p>\n"}, {'answer_id': 21823, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 4, 'selected': False, 'text': "<p>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:</p>\n\n<pre><code>Math.add(2, 3);\n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 21826, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 5, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 21837, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 21845, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': -1, 'selected': False, 'text': "<p>Perhaps a code example would help, I'm going to use C#, but you should be able to follow along.</p>\n\n<p>Lets pretend we have an interface called IPayable</p>\n\n<pre><code>public interface IPayable\n{\n    public Pay(double amount);\n}\n</code></pre>\n\n<p>Now, we have two concrete classes that implement this interface:</p>\n\n<pre><code>public class BusinessAccount : IPayable\n{\n    public void Pay(double amount)\n    {\n        //Logic\n    }\n}\n\npublic class CustomerAccount : IPayable\n{\n    public void Pay(double amount)\n    {\n        //Logic\n    }\n}\n</code></pre>\n\n<p>Now, lets pretend we have a collection of various accounts, to do this we will use a generic list of the type IPayable</p>\n\n<pre><code>List&lt;IPayable&gt; accountsToPay = new List&lt;IPayable&gt;();\naccountsToPay.add(new CustomerAccount());\naccountsToPay.add(new BusinessAccount());\n</code></pre>\n\n<p>Now, we want to pay $50.00 to all those accounts:</p>\n\n<pre><code>foreach (IPayable account in accountsToPay)\n{\n    account.Pay(50.00);\n}\n</code></pre>\n\n<p>So now you see how interfaces are incredibly useful. </p>\n\n<p>They are used on instantiated objects only. Not on static classes. </p>\n\n<p>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.</p>\n"}, {'answer_id': 22497, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 7, 'selected': True, 'text': '<p>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</p>\n\n<pre><code>public interface Foo {\n  public static int bar();\n}\n</code></pre>\n\n<p>and</p>\n\n<pre><code>public interface Foo {\n  public static int bar() {\n    ...\n  }\n}\n</code></pre>\n\n<p>The first is impossible for the reasons that <a href="https://stackoverflow.com/questions/21817/why-cant-i-declare-static-methods-in-an-interface#21826">Espo</a> mentions: you don\'t know which implementing class is the correct definition.</p>\n\n<p>Java <em>could</em> allow the latter; and in fact, starting in Java 8, it does!</p>\n'}, {'answer_id': 138340, 'author': 'Mnementh', 'author_id': 21005, 'author_profile': 'https://Stackoverflow.com/users/21005', 'pm_score': 4, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>public class A {\n   public method x() {...}\n}\npublic class B {\n   public method x() {...}\n}\npublic class C extends A, B { ... }\n</code></pre>\n\n<p>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.</p>\n\n<p>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:</p>\n\n<pre><code>public interface A {\n   public static method x() {...}\n}\npublic interface B {\n   public static method x() {...}\n}\npublic class C implements A, B { ... }\n</code></pre>\n\n<p>Same problem here, what happen if you call C.x()?</p>\n'}, {'answer_id': 520094, 'author': 'Zarkonnen', 'author_id': 15255, 'author_profile': 'https://Stackoverflow.com/users/15255', 'pm_score': 2, 'selected': False, 'text': '<p>There\'s a very nice and concise answer to your question <a href="https://stackoverflow.com/questions/370962/why-cant-static-methods-be-abstract-in-java/370967#370967">here</a>. (It struck me as such a nicely straightforward way of explaining it that I want to link it from here.)</p>\n'}, {'answer_id': 18841163, 'author': 'Lenik', 'author_id': 217071, 'author_profile': 'https://Stackoverflow.com/users/217071', 'pm_score': 2, 'selected': False, 'text': '<p>It seems the static method in the interface might be supported in <a href="http://www.techempower.com/blog/2013/03/26/everything-about-java-8/" rel="nofollow">Java 8</a>, well, my solution is just define them in the inner class.</p>\n\n<pre><code>interface Foo {\n    // ...\n    class fn {\n        public static void func1(...) {\n            // ...\n        }\n    }\n}\n</code></pre>\n\n<p>The same technique can also be used in annotations:</p>\n\n<pre><code>public @interface Foo {\n    String value();\n\n    class fn {\n        public static String getValue(Object obj) {\n            Foo foo = obj.getClass().getAnnotation(Foo.class);\n            return foo == null ? null : foo.value();\n        }\n    }\n}\n</code></pre>\n\n<p>The inner class should always be accessed in the form of <code>Interface.fn...</code> instead of <code>Class.fn...</code>, then, you can get rid of ambiguous problem.</p>\n'}, {'answer_id': 19134923, 'author': 'Sankar', 'author_id': 1690448, 'author_profile': 'https://Stackoverflow.com/users/1690448', 'pm_score': 0, 'selected': False, 'text': '<p>Illegal combination of modifiers : static and abstract</p>\n\n<p>If a member of a class is declared as static, it can be used with its class name which is confined to that class, without creating an object.</p>\n\n<p>If a member of a class is declared as abstract, you need to declare the class as abstract and you need to provide the implementation of the abstract member in its inherited class (Sub-Class).</p>\n\n<p>You need to provide an implementation to the abstract member of a class in sub-class where you are going to change the behaviour of static method, also declared as abstract which is a confined to the base class, which is not correct</p>\n'}, {'answer_id': 20591367, 'author': 'ip_x', 'author_id': 1705343, 'author_profile': 'https://Stackoverflow.com/users/1705343', 'pm_score': 0, 'selected': False, 'text': '<p>Since static methods can not be inherited . So no use placing it in the interface. Interface is basically a contract which all its subscribers have to follow . Placing a static method in interface will force the subscribers to implement it . which now becomes contradictory to the fact that static methods can not be inherited .</p>\n'}, {'answer_id': 22711415, 'author': 'Anandaraja_Srinivasan', 'author_id': 2978567, 'author_profile': 'https://Stackoverflow.com/users/2978567', 'pm_score': 3, 'selected': False, 'text': '<p>Now Java8 allows us to define even Static Methods in Interface.</p>\n\n<pre><code>interface X {\n    static void foo() {\n       System.out.println("foo");\n    }\n}\n\nclass Y implements X {\n    //...\n}\n\npublic class Z {\n   public static void main(String[] args) {\n      X.foo();\n      // Y.foo(); // won\'t compile because foo() is a Static Method of X and not Y\n   }\n}\n</code></pre>\n\n<p>Note: Methods in Interface are still public abstract by default if we don\'t explicitly use the keywords default/static to make them Default methods and Static methods resp.</p>\n'}, {'answer_id': 35220948, 'author': 'Kumar Abhishek', 'author_id': 5772982, 'author_profile': 'https://Stackoverflow.com/users/5772982', 'pm_score': 1, 'selected': False, 'text': '<p>Java 8 Had changed the world you can have static methods in interface but it forces you to provide implementation for that.</p>\n\n<pre><code>public interface StaticMethodInterface {\npublic static int testStaticMethod() {\n    return 0;\n}\n\n/**\n * Illegal combination of modifiers for the interface method\n * testStaticMethod; only one of abstract, default, or static permitted\n * \n * @param i\n * @return\n */\n// public static abstract int testStaticMethod(float i);\n\ndefault int testNonStaticMethod() {\n    return 1;\n}\n\n/**\n * Without implementation.\n * \n * @param i\n * @return\n */\nint testNonStaticMethod(float i);\n</code></pre>\n\n<p>}</p>\n'}, {'answer_id': 54805877, 'author': 'Ishara', 'author_id': 9433614, 'author_profile': 'https://Stackoverflow.com/users/9433614', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>With <strong>Java 8</strong>, interfaces can now have static methods.</p>\n</blockquote>\n\n<p>For example, Comparator has a static naturalOrder() method.</p>\n\n<p>The requirement that interfaces cannot have implementations has also been relaxed. Interfaces can now declare "default" method implementations, which are like normal implementations with one exception: if you inherit both a default implementation from an interface and a normal implementation from a superclass, the superclass\'s implementation will always take priority.</p>\n'}]
['https://Stackoverflow.com/questions/21817', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2238/']