Why using a Nested Class ?

  • It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such “helper classes” makes their package more streamlined.
  • It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A’s members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
  • It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.

Java Sample

package animals;
public class Animals {
    /**
     * @param args the command line arguments
     */
    public String type = "Animal"; 
    static Animals.Alpaca alpaca; 
    static Animals.Llama llama;
    
    public static void main(String[] args) {
        alpaca = new Animals.Alpaca(); // new Alpaca is created
        llama = new Animals.Llama(); // new Llama is created
        Animals animals = new Animals();
        alpaca.getDetailsAlpaca(animals);
        llama.getDetailsLlama(animals);
    }
    public static class Alpaca {
        private final String name;
        public Alpaca() {
            name = "Alpaca";
            System.out.println(name + " is born! " );
        }
        public void getDetailsAlpaca(Animals animals) {
            System.out.println("This is an " + name + ". I am an " + animals.type + " !" );
        } 
    }     
    public static class Llama {
        private final String name;
        public Llama() {
            name = "Llama";
            System.out.println(name + " is born! " );
        }
        public void getDetailsLlama(Animals animals) {
            System.out.println("This is a " + name + ". I am an " + animals.type + " too !"  );
        }    
    }
          
    public Animals() {
        System.out.println("Animals constructor");
    }
}

Output Java Sample

Alpaca is born! 
Llama is born! 
Animals constructor
This is an Alpaca. I am an Animal !
This is a Llama. I am an Animal too !

Grouping Details

  • At their core, static classes exist to allow you to group classes together
  • If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together
  • Java doesn’t allow you to create top-level (non-nested) static classes, so static classes will always be nested.

Instantiation Details

  • Referring to the sample code above, the important distinction to make is that
  • Creating an Animals object does not create an Alpaca or Llama object.
  • To instantiate the nested static class, we have to use new on the Animals class itself .
  • The nested static class Alpaca acts like its own class, but the only thing is, it must be accessed via the Animals top-level class
  • An instance of the top-level class is not needed to create an object of the nested class.

Level of access Details

  • Nested static classes are able to access static data members of the top-level class (and call static methods)
  • Nested static classes are unable to access non-static data members since you can create an instance of a static inner class without creating any instance of the outer class.
  • A static nested class interacts with the instance members of its outer class just like any other top-level class.

Reference