Inner classes are best for the purpose of logically grouping classes that are used in one-place. For example, if you want to create class which is used by ONLY enclosing class, then it doesn’t make sense to create a separate file for that. Instead you can add it as “inner class”
As per java tutorial:
Compelling reasons for using nested classes include the following:
- It is a way of logically grouping classes that are only used in one
place.- It increases encapsulation.
- It can lead to more readable and maintainable code.