Nested Classes

Nested Classes

class OuterClass {
    ...
    static class StaticNestedClass {
        ...
    }
    class InnerClass {
        ...
    }
}
  • The Java programming language allows you to define a class within another class. Such a class is called a nested class.
  • Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes.
  • A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.).
  • Reasons for using nested classes include the following: it is a way of logically grouping classes that are only used in one place, it increases encapsulation, it can lead to more readable and maintainable code.

Nested Classes — Structure map

Clickable & Draggable!

Nested Classes — Related pages: