Definition of Inner class:
A class defined in another class is known as an inner class. Inner classes are categorized into four types.
1. Member inner classes
2. Static inner classes
3. Local inner classes
4. Anonymous inner classes
Member inner classes:
If we define a class in another class just as a member like variable and functions of those classes, then we call such a class as member inner class.
Static inner classes:
If we define any inner class as static then it is known as static inner class. We can define any access specifier before a static inner class. Static inner classes can have static members as well as non–static members. Static members of a class are directly available to the static and non-static members of that class but non-static members of a class are not directly available to the static members of that class.
Local inner classes:
When we define a class, local to the function, just as a local variable, then it is known as a local inner class. Local variables of a function are not accessible from the local inner classes even though the local inner class is defined inside the function.
Anonymous inner classes:
Anonymous inner classes are the inner classes defined without the name. In order to define Anonymous inner classes, we definitely need an interface or an abstract class. Anonymous inner classes are two types
- Local anonymous inner classes
- Member anonymous inner classes.
Uses of inner classes:
We can achieve more modularity using inner classes. Whenever we need to define a large number of functions in a class, then we go for inner classes.
If we have class mathematics, then instead of defining all the functions in that single class, we can group functions of topics individually.