Can we create class inside class?

In Java, it is possible to define a class within another class , such classes are known as nested classes. They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation, and creates more readable and maintainable code.

Read more

Why nested classes are needed?

As mentioned in the section Nested Classes, nested classes enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code. … Use it if you are encapsulating a single unit of behavior that you want to pass to other code.

Read more