Why can't I define a static method in a Java interface?

后端 未结 24 1535
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 05:44

EDIT: As of Java 8, static methods are now allowed in interfaces.

Here\'s the example:

public interface IXMLizable         


        
24条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 06:33

    To solve this : error: missing method body, or declare abstract static void main(String[] args);

    interface I
    {
        int x=20;
        void getValue();
        static void main(String[] args){};//Put curly braces 
    }
    class InterDemo implements I
    {
        public void getValue()
        {
        System.out.println(x);
        }
        public static void main(String[] args)
        {
        InterDemo i=new InterDemo();
        i.getValue();   
        }
    
    }
    

    output : 20

    Now we can use static method in interface

提交回复
热议问题