Nested Classes

This lesson explains nested classes in Java.

We'll cover the following...

Question # 1

What are nested classes?

Java allows defining a class within another class. A nested class is a member of its enclosing class. Nested classes can be either static or non-static.

  • Static Nested Class
  • Non-Static Nested Class or Inner Class. The following classes are also considered inner classes.
    • Local Class
    • Anonymous Class

The below code snippet shows an outer class declaring a static and a non-static nested class.

Static and Non-static Nested Classes

public class OuterClass {

    String myName = "outerclass";
    private static String staticName = "outerclass";

    private class InnerClass {

       
...