What are The Nested Classes in Java?

Java allows programmers to define a class inside another class. This concept helps in grouping classes logically. Nested classes are those classes that are defined or created within another class. It is to be noted that the lifetime and scope of the nested class remains bounded with the scope of its enclosing class.

Moreover, it has many advantages as well. Nesting of classes increases the usefulness of encapsulation. Also, the readability and maintainability of code increase with the use of nesting of classes.

Furthermore, it has full access to its members, both public as well as private members. Nested classes are also treated as members of its encircled class. It is of two types. These are:

  • static nested class: they are declared using the keyword static
  • inner class: classes those are not declared as static (non-static)

Nested Class Structure

class OuterClass
{
....
     class NestedClass
     {
     ....
     }
}

Here are few other related articles for you to read:

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.