Search code examples
javaclassstaticstatic-classes

Why two different ways to create instance of Inner classes?


Now that I was looking into static inner class, I found that the way of object creation of static inner class and non-static inner class is different. But I don't understand why.

For non-static inner class:

Outer.Inner inner = new Outer().new Inner();

For static inner class:

Outer.Inner inner = new Outer.Inner();

Solution

  • The entire point of a non-static class is that it's linked to an instance of the outer class.

    That's why you need to create it from an instance.