Java nested class overview
When we declares a class within the body of another class or interface then class is called as nested class.
Three types of inner classes -
There are two types of nested classes : static inner classes and non-static nested classes. Non-static nested classes are called inner classes. Three Types of the Inner Classes are listed below -
- Member inner classes
- Local inner classes
- Anonymous inner classes
Example of Nested Class :
public class OuterClass { class NestedClass { } }
In the above example we can say that InnerClass is class which is enclosed in the OuterClass.
Advantages of Using Nested Classes :
These are the benefits of inner classes:
- Inner classes can access all members of the Outer Class including Privet Members.
- Inner classes help you hide the implementation of a class completely.
- Inner classes provides a shorter way of writing listeners.
- Inner classes helps us to write optimized code.