Inner class in Java2 min read

What is inner class in Java?

An inner class in Java is any class whose definition is inside the definition of another class. Inner classes can be either named or anonymous. It is possible to define a class within another class in Java, and these classes are referred to as nested classes. They allow you to logically arrange classes that are only used in one place, increasing encapsulation and making code more readable and maintainable.

  • A nested/inner class’s scope is limited by the scope of the enclosing class. As a result, class NestedClass does not exist independently of class OuterClass in the example below.
  • Inner classes in Java have access to the members of the class in which it is nested, including private members. The opposite is not true, in that the enclosing class does not have access to the nested class’s members.
  • The enclosing class is also a member of the nested class.
  • A nested class can be marked private, public, protected, or package-private as a member of its enclosing class (default).

Example:

class OuterClass{ // outer class
  // definition
  class InnerClass{ // inner class
    // definition
  }
}
  • In the above example, class InnerClass is defined in class OuterClass. The scope of the inner class is in the scope of the outer class.
  • The outer class knows the members of the inner class but the members of the outer class are not accessible inside the inner class.

Types of Inner Classes

Non-static inner classes – These are defined as an instance member of another class, just like instance variables and method members are defined in a class. The instance of a non-static inner class always has an instance of the enclosing class associated with it.

Static inner classes – There may be a static nested class object if an outer class object does not exist. That is, the outer class object is not linked to the static nested class object.

Non-static inner class

Example:

class Outer{
  // top-level class
  // private members
  // public member
  class Inner{
    // non-static inner
    // private member
    // public members
  }
}

Static inner class

An inner class can also be static, allowing you to access it without having to create an instance of the outer type.

Example:

class OuterClass {
  int x = 10;
  static class InnerClass {
    int y = 5;
  }
}

public class Main {
  public static void main(String[] args) {
    OuterClass.InnerClass myInner = new OuterClass.InnerClass();
    System.out.println(myInner.y);
  }
}

Output:

5

Local classes

  • These are defined in a block of code as in a method body or a local block, just like a local variable. Local classes cannot have static members, as they cannot provide class-specific services.
  • Local classes cannot have any accessibility. It can access members defined within the class.
class SuperA{
  // members
}

class TopLevel{
  void method1(){
    // members
    class LocalA extends SuperA{
      // members
    }
  }
  static void method2(){
    // members
    class LocalB extends SuperA{
    }
  }
}

Share:

Leave a Reply