1

After java 8 it is known that interfaces can have static methods and default methods.

Below is the example :

interface interA{
    static void method()
    {
        System.out.println("Static method inside interface");
    }
    public default void AImp(){
        System.out.println("Calling Aimp from interA");
    }
}

Now my question is that why is static initialization block not allowed in interface.?

Example :

interface interA{
    static void method()
    {
        System.out.println("Static method inside interface");
    }
    static
    {
    }
    public default void AImp(){
        System.out.println("Calling Aimp from interA");
    }
}
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Sushmi S
  • 61
  • 1
  • 1
  • 6
  • 1
    what do you want to initialize in the block? – luk2302 May 20 '18 at 14:39
  • Why are you providing implementation of methods in interfere? Interfce holds only signature of methods. – Parth Patel May 20 '18 at 14:42
  • @ParthPatel - As of Java 8, interfaces can have [default methods](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html). – T.J. Crowder May 20 '18 at 14:44
  • I agree, with the addition of `default` methods the difference between interfaces and 'real' classes has become less obvious. Java started out with ensuring a single inheritance tree, and interfaces were [adopted from Objective-C's](https://cs.gmu.edu/~sean/stuff/java-objc.html) equivalent [protocol](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html). With default methods, interfaces seem to become classes. – M. le Rutte May 20 '18 at 14:47
  • (@SushmiS - Ignore my earlier, deleted comment, copy/paste error.) – T.J. Crowder May 20 '18 at 14:53
  • @luk2302 I may want to print anything inside the block . My question is that why is it not allowing Static initialization block but allowing static methods alone. – Sushmi S May 21 '18 at 05:24

0 Answers0