Search code examples
javamemory-managementstatic-class

Inner static class in Java


What is benefit of using an inner static class? Where should I prefer it over other options?

And how is its memory allocated?


Solution

  • If the inner class is static, you don't need an instance of the outer class to instantiate it.

    If the inner class is public, it's basically just a name-scoping technique for highlighting the fact that the class "belongs" to the outer class.

    If you make the inner class private however, it can't be used outside of that class.