Nested and Inner Classes in Java

Java allows us to define one class inside another class. This process is called nesting of classes and the internal class is called a nested class.

The nesting can go on up to any level. For simplicity and clearity of understanding we will be discussing two level nesting i.e one class nested into the other.


1. The Syntax to define a Nested class

A nested class is defined like any other class, but inside a class. You can give them any of the four access levels.  In the example below the Inner class has default access level.

 


2. Nested classes vs Outer classes

Two major differences between an outer class and a nested class are:

  • An outer class cannot be declared static but a nested class can be declared static.
  • An outer class can be either public or it can have default access level while the nested class can have all four access levels(public, private, protected, default).

3. Two Categories of Nested Classes

Based on use of static keyword, nested classes can be divided into two categories:

  1. Static nested classes : A nested class which is qualified with static keyword.
  2. Non-static nested  classes (also called Inner Classes) : A nested class which has not been qualified with static keyword.

Let’s look at them one at a time.


4. Static Nested Classes

A nested class which has been qualified with the static keyword is called a static nested class. Some features of static nested classes are:

  • Static classes can be accessed using the class name directly. No objects of the outer class has to be created.
  • It can access only the static members of the outer class directly. A non-static member can be accessed only through an object instantiation of the outer class.

4.1 Static Classes can be accessed using the class name

You can easily make an instance of the static nested class by using their class name :

Let’s look at an example:

Output :


4.2 A static Nested class and static members of outer class

A static nested class can only access the static members of outer class directly. The non-static members cannot be accessed directly. You need to make an object to the outer class to access them.

Here is a slightly modified version of the last example. The color field of the outer class has been declared static and a new field type has been added:

Output :

 


5. Inner Classes

A non-static nested class is also called an inner class because they truly belong to an object instance. Objects of an inner class exist in an instance of the outer class. So you have to create an object of the outer class first to create any instances of the inner class.


5.1 Example : An inner class

Outer class SolarSystem contains an inner class Planet.

Output :

Planet p1 and p2 exist inside the object of the outer class named ob.

Notice two things here :

  • how type of the inner class is defined(i.e. OuterClass.InnerClass) and
  • how the object instance of the inner class is created. (i.e OuterClassObject.new InnerClassConstructor)

5.2 An inner class can access other members of the outer class

Unlike a static nested class, a non-static nested class can access all the other static and non-static members of the outer class. It can even access the private members of the outer class.

Output :

Notice a few things :

  • Being a member of the class, the inner class can access other static(RAM variable) as well as non-static(type variable) members of the class.
  • Even the private variables and methods are accessible from inside the inner class.

6. Why should classes be Nested

There are mainly three reasons:

  • It improves encapsulation : By nesting a class inside the other and declaring the nested class private you can hide this class from the world.
  • It improves readability of the code : Looking at the context in which a class is defined, it is better to understand the way a class is defined.
  • It improves the logical grouping of the classes : Suppose a class X is only used in context of some other class Y, then it is better to nest X inside Y. For example CPU class was nested inside Computer class.

7. Points to Remember

  • Outer classes vs Nested classes :
    • An outer class cannot be declared static and it can have either public or default access level.
    • A nested class can be declared static and it can have all four access levels.
  • Nested classes can be divided into two categories:
    • Static nested class and
    • Non-static nested class(also called inner class)
  • Accessibility : 
    • Static classes can only access the static data of the outer class.
    • An inner class can access both the static and non-static data of the outer class.
  • Instantiation : 
    • You can use the class names direclty to instantiate a static nested class.
    • To instantiate an inner class you have to instantiate the outer class first.