Nested classes in java

Let’s learn nested classes in java.

Nested classes in java

Defining a class within another class is known as nested class. Nested classes helps in encapsulating java code in one place.

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 helps in maintaining code. Hence more readable.

There are two types of nested classes in java, they are,

  • static nested class
  • non-static nested class (inner class)
nested classes in java

static nested class in java

  • static nested class is created inside an outer class.
  • static nested class is declared with keyword ‘static’.
  • static nested class cannot access non-static methods and members.
  • static nested class can access outer class data members along with private members.

Static nested class syntax:

class OuterClass
{
   static class StaticNestedClass 
   {

   } 
}

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


Here’s a program on java static nested class with instance method,

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 
   {
      // instance method
      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

In the above example we have not created instance of static nested class “StaticNestedClass” since it has instance method print().

Meanwhile we have not created object of “OuterClass”. Because “StaticNestedClass” is a static class and static methods and static classes can accessed without creating object.

static nested class example with static method

Here in this case we are not creating instance of “StaticNestedClass”. Because static method is inside “StaticNestedClass”.

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 
   {
      // static method
      static 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) 
   {
      // we are not creating instance of 'StaticNestedClass'
      OuterClass.StaticNestedClass.print();
   }
}



Output:

Outer static member = 200
Outer private member = 700


Non static nested class or Inner class

Non-static nested class or inner class is declared without keyword “static”. A non-static nested class can access members of outer class.

Also read – inheritance in java

Here, first we have to instantiate outer class in order to instantiate inner class.

NOTE:

  1. inner class or non-static nested class can have any access modifiers protected, private etc. since inner class is a member of outer class.
  2. In java, inner classes have access to class members of outer class even if members are declared with private access specifier.

There are two types of inner classes in java,

  • Local inner class in java
  • Anonymous inner class in java

Syntax:

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

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


Let’s see an example on non-static nested class or 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 in java

A class defined inside a method is called as local inner class. Here class instantiation is done inside the method and accessible within that method.

NOTE:

  1. local inner class variables can’t be protected, package and public.
  2. local inner class can’t be invoked outside method.
  3. local inner class can access members of enclosing class.
  4. local inner class can also access local variables inside same method only if variables are declared with keyword ‘final’.
public class LocalInner 
{
   private double number = 566.56;
   void display()
   {
      // local inner class
      class Inner
      {
         void print()
         {
            System.out.println(number);
         }
      }
      // instantiating Inner 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 in java

Inner class without a class name is anonymous inner class in java.

Syntax:

Demo obj = new Demo()
{
   public void display()
   {
      
   }
};

Example:

public class A
{
   public void hello()
   {
      System.out.println("hello world");
   }
}

A obj = new A()
{
   public void hello()
   {
      System.out.println("anonymous hello world");
   }
};
obj.hello();

OUTPUT: anonymous hello world


Now let’s see java program for anonymous inner class,

interface Anonymous
{
   int number = 665;
   void print();
}

class Demo implements Anonymous
{
   @Override
   public void print()
   {
      System.out.println("hello world - " + number);
   }
}

public class AnonymousDemo 
{
   public static void main(String[] args) 
   {
      // class Demo is implementing class of interface Anonymous
      Demo obj = new Demo();
      // call print() method implemented in class Demo
      obj.print();
   }
}



Output:

hello world – 665