1

In the code below, we can prove that interfaces can have static class and non-static class declared inside (static nested class and inner class). OK, no new. But, what is the difference of "nested" and "inner" classes (inside interfaces) if "inner" class can have a instance without a instance of the outer class, even in the Interface Implemtations (MyInterfImpl in the code below)?

I have commented the lines on the code, to "highlight":

public class NewTest {

    public static void main(String[] args) {

        // OK Compilation Fail on next line, here we can't have an Instance of inner class because we don't have
        // instance of outer class, nothing strange to me
        MyClass.MyInner inner = new MyClass.MyInner(); //COMPILE FAIL:  must be new MyClass().new MyInner() instead
        MyClass.MyNested nested = new MyClass.MyNested(); // OK here, its nested

        MyInterf.MyInner inner2 = new MyInterf.MyInner(); // HERE, MyInner is Inner class, ins't it? 
        MyInterf.MyNested nested2 = new MyInterf.MyNested(); // OK here, its nested

        MyInterfImpl.MyInner inner3 = new MyInterfImpl.MyInner(); // // HERE, MyInner is Inner class, ins't it? 
        MyInterfImpl.MyNested nested3 = new MyInterfImpl.MyNested(); // // OK here, its nested

    }




}

interface MyInterf{

    public static class MyNested{

    } // end

    public class MyInner{

    }//

    // some abstract methods....
}

class MyClass{
    public static class MyNested{

    } // end

    public class MyInner{

    }// end
}

class MyInterfImpl implements MyInterf{

}
G Bisconcini
  • 764
  • 2
  • 6
  • 25
  • class in an interface?? common.. AFAIK, `public` and `static` modifiers are superfluous in an interface –  Apr 26 '16 at 20:37
  • Hi I'm not sure if I understood correctly your question, so if not please let me know. the general contract for using static methods does not require the new keyword, which is designed for creating instances. so we could expect MyClass.MyInner(); to work, but it will not compile too, since MyInner is not static, and then it should be available only with instances scope, then it will work again using: new MyClass().MyInner(); On the other hand new MyInterf.MyNested() will work, because interfaces declares things as implicitly static. (as Thufir said too) Hope that helps – Alfredo Serafini Apr 26 '16 at 20:39

1 Answers1

1

From this answer by Nad Nik: Classes inside interfaces are actually implicitly both static and public. So MyInterf.MyInner() works fine, since MyInterf.MyInner is actually static.

Edit Thanks to Andreas for pointing out in the comment that MyInterf.MyInner is a static nested class, not an inner class.

Source: Java spec, sec. 9.5

Friend, I hope you are not sitting with your back to any doors. :)

Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81
  • Why didn't you vote close as duplicate? –  Apr 26 '16 at 20:41
  • 1
    *"Inner classes inside interfaces are actually implicitly static"* is a contradiction in terms. An *inner class* is defined as being a *non-static nested class*. Being `static` means that is cannot be an *inner class*, but is a *nested class*; more specifically a *static nested class*. See [JLS 8.1.3 - Inner Classes and Enclosing Instances](https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.3): An *inner class* is a nested class that is **not** explicitly or **implicitly declared `static`**. – Andreas Apr 26 '16 at 20:52
  • @Andreas updated - thanks! – cxw Apr 26 '16 at 21:00
  • 2
    @RC. because (1) Thufir already knew the answer to the linked question :) and (2) Nad Nik's answer, which seemed to me the most pertinent, was quite a ways down the page, and I didn't want to send Thufir hunting for the answer. If the accepted answer to the linked question had also answered the question here, I would have probably voted duplicate. However, I've never stood for election as a moderator, so... ;) – cxw Apr 26 '16 at 21:03