Java nested class overview

When we declares a class within the body of another class or interface then class is called as nested class.
Types of Nested Classes

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 -

  1. Member inner classes
  2. Local inner classes
  3. 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:

  1. Inner classes can access all members of the Outer Class including Privet Members.
  2. Inner classes help you hide the implementation of a class completely.
  3. Inner classes provides a shorter way of writing listeners.
  4. Inner classes helps us to write optimized code.