example of nested class in java

Example 1: Nested classes in java

// example on inner class in java
class OuterDemo
{
   int num;
   // inner class
   private class InnerDemo
   {
      public void display()
      {
         System.out.println("inner class");
      }
   }
   // access inner class from method within
   void printInner()
   {
      InnerDemo objInner = new InnerDemo();
      objInner.display();
   }
}
public class InnerClassExample
{
   public static void main(String[] args)
   {
      OuterDemo objOuter = new OuterDemo();
      objOuter.printInner();
   }
}

Example 2: use of nested class in java

In Java, it is possible to define a class within another class, such 
classes are known as nested classes. They enable you to logically group 
classes that are only used in one place, thus this increases the use of 
encapsulation, and creates more readable and maintainable code.

Tags:

Misc Example