Classes Nested in an Interface

Why is it possible to define internal (aka non-static nested) classes in an interface?

Is there any reason? They cannot exist in an instance of the containing interface, because interfaces cannot be created, therefore ...

The following will compile:

interface MyInterface
{
    static class StaticNestedClass
    {
        static int a()
        {
            return 0;
        }
        int b()
        {
            return 1;
        }
    }
    class InnerClass
    {
        static int a()
        {
            return 0;
        }
        int b()
        {
            return 1;
        }
    }
}

Is there a difference between the two above classes? Is it really statictaken into account? Please note: if you change interfaceto class, you will obviously get a compilation error at InnerClass' static int a().

Also, take a look at the following:

interface MyInterface
{
    int c=0;
    static class StaticNestedClass
    {
        static int a()
        {
            return c;
        }
        int b()
        {
            return c+1;
        }
    }
    class InnerClass
    {
        static int a()
        {
            return c;
        }
        int b()
        {
            return c+1;
        }
    }
}

, , , , , " ( ) , ", , , , c integer ... interface ?

, : StaticNestedClass InnerClass ?

+4
1
class InnerClass 

( JLS, 9.5)

. .

static class InnerClass

.

, , .

,

, StaticNestedClass InnerClass

+3

All Articles