Nested classes in java

Let’s learn nested classes in java.

Java nested class

Nested classes java or java inner class is a class within another class.

Java nested class syntax:

class A  // outer class
{
   ......
   class B   // nested class 
   {
      .....
   } 
}


NOTE:

  1. Nested class is a member of its enclosing class. Hence scope of nested class is constrained by its Outer class.
  2. Access modifiers for nested class are public, protected, private or package private.
  3. package private is the default access modifier for nested class.
  4. Outer class does not have access to the members of nested class. While nested class has access to members, also private members of Outer class.
  5. Nested class group classes logically thus increasing the use of encapsulation.
  6. Types of nested class java
  • static nested class
  • inner class java.
nested classes in java

Static nested class java

  • Static nested class is a member of outer class.
  • Outer class can have static members. But when an inner class is declared with keyword “static” then this class will become static member to outer class.
  • Like static members we need not instantiate outer class to call static members of static nested inner class.
  • Static nested class cannot access instance variable of outer class.

Static nested class java syntax:

class OuterClass
{
   static class StaticNestedClass 
   {

   } 
}

OuterClass.StaticNestedClass obj = new OuterClass.StaticNestedClass();


Here’s a program on java static nested class,

public class OuterClass 
{
   // static member 
   static int outerStaticNumber = 200;
   // non-static member 
   int outerNonStaticNumber = 600;
   // private member 
   private static int outerPrivateNumber = 700; 
   // static nested class 
   static class StaticNestedClass 
   { 
      void print() 
      { 
         // accessing static member of outer class 
         System.out.println("Outer static member = " + outerStaticNumber);
         // printing private static member of outer class 
         System.out.println("Outer private member = " + outerPrivateNumber);
      } 
   }
}


public class StaticNestedDemo 
{
   public static void main(String[] args) 
   {
      OuterClass.StaticNestedClass obj = new OuterClass.StaticNestedClass();
      obj.print();
   }
}



Output:

Outer static member = 200
Outer private member = 700


Java inner class or Non static nested class

Java inner class declared without “static” keyword is non-static nested class. A non-static inner class can be declared only with non-static members.

Methods in non-static inner class can access all the members (static and non-static) without reference.

Also read – inheritance in java

All the static and non-static members of outer class can be accessed by methods of non-static inner class directly.

There are two types of java inner class,

  • Local inner class
  • Anonymous inner class

Syntax:

class OuterClass
{
   ......
   class InnerClass 
   {
      .....
   } 
}


OuterClass.InnerClass innerObj = outerObj.new InnerClass();


Let’s see a java program on java inner class,

// outer class
class Outer
{
   // non-static variable
   int number = 100;
   // static variable
   static int even = 4;
   // private variable
   private int outerPrivateNumber = 66;
   // java inner class
   class Inner
   {
      void print()
      {
         // accessing static variable of outer class
         System.out.println("static variable : " + even);
         // accessing non-static variable of outer class
         System.out.println("non-static variable : " + number);
         // accessing private variable of outer class
         System.out.println("Private variable : " + outerPrivateNumber);
      }
   }
}


public class NonStaticNestedDemo 
{
   public static void main(String[] args) 
   {
      Outer obj1 = new Outer();
      Outer.Inner obj2 = obj1.new Inner();
      obj2.print();
   }
}



Output:

static variable : 4
non-static variable : 100
Private variable : 66


Also read – learn static keyword in java


Local inner class

A class defined inside a method of outer class is called as local inner class or method local inner class. For example,

// outer class
public class LocalInner 
{
   private double number = 566.56;
   void display()
   {
      // local inner class
      class Inner
      {
         void print()
         {
            System.out.println(number);
         }
      }// end LocalInner class
      // instantiating LocalInner class
      Inner obj = new Inner();
      obj.print();
   }

   public static void main(String[] args) 
   {
      LocalInner obj1 = new LocalInner();
      obj1.display();
   }
}


Output:

566.56


Anonymous inner class

A class without definition and name inside a method is anonymous inner class in java. For example,

interface Anonymous
{
   void print();
}

// outer class
public class AnonymousDemo
{
   public static void main(String[] args) 
   {
      // anonymous inner class
      Anonymous obj = new Anonymous()
      { 
         @Override
         public void print() 
         {
            System.out.println("this is anonymous inner class");
         }
      }; // end of anonymous inner class
      obj.print();
   }
}



Output:

this is anonymous inner class

NOTE : Anonymous inner class is used to provide definition for interfaces.