10

The member interface can only be defined inside a top-level class or interface or in a static context.

Case A: Interface within a top-level class works perfectly

package multiplei.interfaces.test;

public class InterfaceBetweenClass {

    interface Foo {
        void show();
    }

    class InnerClass implements Foo{
        public void show(){
            System.out.println("Inner Class implements Foo");
        }
    }

    public static void main(String[] args) {
        new InterfaceBetweenClass().new InnerClass().show();
    }

}

Case B: Interface within an interface works good.

public interface Creatable {
    interface Foo{
        void show();
    }}

Case C: I know it sounds stupid that why would anyone define an interface in a static context. But it give me the same error message when i try to define the interface in static context.

package multiplei.interfaces.test;

public class InterfaceBetweenClass {
    public static void main(String[] args) {
        interface Foo {  //Line 5
            void show(); 
        }
    }

}}

But line number 5 gives me the following error message "The member interface Foo can only be defined inside a top-level class or interface or in a static context." Please help me out with this If an interface can be defined in static context then how?

Yati Sawhney
  • 1,372
  • 1
  • 12
  • 19
  • Even if it were possible it would not make any sense to create an interface within a method. This interface would not be visible outside the methods scope, not even in anonymous inner classes create later in the same method. – Timothy Truckle Mar 13 '17 at 08:28
  • @TimothyTruckle I don't know about that. It seems to be the same situation as with "local classes" and those are allowed within methods: http://stackoverflow.com/questions/2428186/use-of-class-definitions-inside-a-method-in-java?noredirect=1&lq=1 – Thilo Mar 13 '17 at 08:39
  • 1
    @Thilo *'It seems to be the same situation as with "local classes"'* not quite. You create local class for immediate use within the same method (and nowhere else) whereas you create interfaces for use at *some other place* instead of some concrete class implementing it. – Timothy Truckle Mar 13 '17 at 08:42
  • 1
    @TimothyTruckle You might create a "local interface" and have three "local classes" implement it. The calling code (also in the same method) could benefit from that. Note that you *can* have abstract local classes and local classes can extend from other local classes. – Thilo Mar 13 '17 at 09:06

2 Answers2

4

You cannot define interfaces within methods.

I think the scenario the error message is referring to is defining an interface inside an inner class (which can be done, but only if that is a static inner class):

class A{
   static class X{
     interface Y{}
   }
}
Thilo
  • 257,207
  • 101
  • 511
  • 656
0

For interface, both nested-interface and member-interface are the same thing (#opposite to member-class). Moreover, member-interface is an interface which is DIRECTLY enclosed by another class or another interface. Therefore, local interface DOES NOT exist.

Clara
  • 1