AppDividend
Latest Code Tutorials

Nested class In Java Example | Java Nested Class Tutorial

0

Nested class In Java Example | Java Nested Class Tutorial is today’s topic. In java, a Nested class is defined when a class is present/define within another class. The scope of the nested class is defined within the curly bracket. In java, it is possible to define the class within another class; such classes are known as nested classes.

Nested class In Java

Nested Class enable you to logically group classes that are only used in one place; thus, it increases the use of encapsulation, and create more readable and maintainable code.

#Features of nested class

  1. The nested class can access the member of the upper class, but the upper class cannot access the member of the nested class in java.
  2.  A class is also called as the 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 as an inner class in which the inner class has all the rights to access the member of the outer class. 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 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 and 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 static class, it needs the access to non-static members of its enclosing class through the object.
  2. Static classes cannot refer to non-static members of its enclosing class directly 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 its outer class and may refer to them directly in a same way that the other non-static members of the outer class do.

Finally, Nested class In Java Example | Java Nested Class Tutorial is over.

Leave A Reply

Your email address will not be published.

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