Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 617
Labeeb Panampullan Avatar asked Nov 15 '10 09:11

Labeeb Panampullan


People also ask

What is static inner class in Java?

A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.

Can we use static in inner class in Java?

Note: In Java, only nested classes are allowed to be static. Static nested classes are associated with the outer class.

What is the advantage of static inner class in Java?

If the nested class does not access any of the variables of the enclosing class, it can be made static. The advantage of this is that you do not need an enclosing instance of the outer class to use the nested class.

What is an inner class in Java?

In Java, inner class refers to the class that is declared inside class or interface which were mainly introduced, to sum up, same logically relatable classes as Java is purely object-oriented so bringing it closer to the real world.


1 Answers

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.

like image 99
aioobe Avatar answered Oct 21 '22 07:10

aioobe