Nested class In Java: The Complete Guide

Java Nested class is defined when a class is present/defined within another class. The scope of the nested class is defined within the curly bracket. It is possible to define the class within another class; such classes are known as nested classes.

Nested class In Java

Java nested class enables you to logically group classes that are only used in one place; thus, it increases the use of encapsulation and creates a more readable and maintainable code.

Features of nested class

  1. The nested class can access the upper-class member, but the upper class cannot access the member of the nested class in Java.
  2.  A class is also called a member of the enclosing class.
  3.  We can declare a nested class as private, public, protected, or default.

Syntax

See the following syntax.

class outer {
  // statements of outer class
  class inner {
    // statements of inner class
  }
} // outer close

Types of Nested Class

  1. Non-static nested class (inner class)
  2. Static nested class

Non-static Nested class

A non-static nested class is also called an inner class in which the inner class has all the rights to access the member of the outer class. For example, the inner class can access the private, protected, or default member of the outer class, but the reverse is not possible, i.e., an outer class cannot access the member of the inner class. The inner class is always created within the scope of the outer class.

It is again divided into two categories:

  1. local class
  2. anonymous class
  3. Method-local Inner Class

Program for inner class

class outer {
  private int a = 1; // private member

  static int b = 2; // static member

  int c = 3; // non-static member

  class inner {
    void show() {
      System.out.println("private member of outer class=" + a);

      System.out.println("static member of outer class=" + b);
      System.out.println("non-static member of outer class=" + c);
    } // method close
  } // inner class close
} // outer class close

class nested {
  public static void main(String args[]) {
    outer obj = new outer();
    outer.inner obj1 = obj.new inner();
    obj1.show();
  }
}

See the output.

Nested class In Java

Static nested class

A static nested class is a class that is defined within another class with a static modifier with the class name as the class is defined with a static modifier so it can access only the static member of the outer class but not the non-static member.

As with class methods and variables, the static nested class is associated with its outer class. Like static class methods, the static nested class cannot refer directly to the instance variables or methods defined in its enclosing class. It can use them only through the object reference.

The static inner class is the nested class, which is the static member of the outer class. It can be accessed without instantiating an outer class, using other static members. Just like static members, the static nested class does not have access to the instance variables and methods of the outer class.

Syntax

Outer classname.inner classname object_name=new outer classname.inner classname( );

Program for static class

class outer {
  private static int a = 1;

  static int b = 2;

  int c = 3;

  static class inner {
    void show() {
      System.out.println("private member of outer class=" + a);
      System.out.println("static member of outer class=" + b);
      // System.out.println("non-static member of outer class="+c); //this statement
      // is not ligal as the innerclass is static and outerclass is non-static
    }
  }
}

class static_nested {
  public static void main(String args[]) {
    outer.inner obj = new outer.inner();
    obj.show();
  }
}

See the output.

Static nested class

#Difference between static and inner or non-static nested classes

  1. The static nested classes do not directly have access to the other members(non-static variables and methods) of an enclosing class because, as it is a static class, it needs access to non-static members of its enclosing class through the object.
  2. Static classes cannot directly refer non-static members of their enclosing class because of this restriction, and static nested classes are seldom used.
  3. Non-static nested classes or inner classes have access to all members(static and non-static variables and methods, including private) of their outer class. They may refer to them directly in the same way that the other non-static members of the outer class do.

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.