Java Java Tutorial Java Methods Java Classes Java File Handling Java Reference



Java Inner Classes

Java is an object-oriented programming language that allows developers to create classes and objects. Inner classes are a feature of Java that allows developers to define a class within another class. Inner classes are also known as nested classes because they are nested within another class. Inner classes are used to group related classes together and to improve code organization. In this article, we will discuss Java inner classes in detail.

Brief Explanation of Java Inner Classes

Java inner classes are classes that are defined within another class. Inner classes are used to group related classes together and to improve code organization. Inner classes can be divided into four types:

  • Member inner classes
  • Local inner classes
  • Anonymous inner classes
  • Static nested classes

Member Inner Classes

Member inner classes are defined within a class and are associated with an instance of the outer class. Member inner classes can access all the members of the outer class, including private members. Member inner classes are declared using the keyword "class" inside the body of the outer class. Here is an example:


public class OuterClass {
  private int x = 10;

  class InnerClass {
    public void printX() {
      System.out.println("x = " + x);
    }
  }
}

In the above example, the InnerClass is a member inner class of the OuterClass. The InnerClass can access the private member x of the OuterClass.

Local Inner Classes

Local inner classes are defined within a method or a block of code. Local inner classes are not associated with an instance of the outer class. Local inner classes can access final variables and parameters of the method or block in which they are defined. Local inner classes are declared using the keyword "class" inside a method or block of code. Here is an example:


public class OuterClass {
  public void printMessage(final String message) {
    class InnerClass {
      public void print() {
        System.out.println(message);
      }
    }
    InnerClass inner = new InnerClass();
    inner.print();
  }
}

In the above example, the InnerClass is a local inner class defined inside the printMessage method. The InnerClass can access the final variable message of the printMessage method.

Anonymous Inner Classes

Anonymous inner classes are defined without a name. Anonymous inner classes are used to create objects of a class that implements an interface or extends a class. Anonymous inner classes are declared using the keyword "new" followed by the name of the interface or class being implemented or extended. Here is an example:


public class OuterClass {
  public void printMessage() {
    Runnable runnable = new Runnable() {
      public void run() {
        System.out.println("Hello, world!");
      }
    };
    Thread thread = new Thread(runnable);
    thread.start();
  }
}

In the above example, an anonymous inner class is used to create an object of the Runnable interface. The run method of the anonymous inner class prints "Hello, world!" to the console.

Static Nested Classes

Static nested classes are defined within a class and are associated with the outer class. Static nested classes can access only static members of the outer class. Static nested classes are declared using the keyword "static" inside the body of the outer class. Here is an example:


public class OuterClass {
  private static int x = 10;

  static class InnerClass {
    public void printX() {
      System.out.println("x = " + x);
    }
  }
}

In the above example, the InnerClass is a static nested class of the OuterClass. The InnerClass can access the static member x of the OuterClass.

Code Examples

Here are some code examples that demonstrate the use of Java inner classes:

Member Inner Class Example


public class OuterClass {
  private int x = 10;

  class InnerClass {
    public void printX() {
      System.out.println("x = " + x);
    }
  }

  public static void main(String[] args) {
    OuterClass outer = new OuterClass();
    OuterClass.InnerClass inner = outer.new InnerClass();
    inner.printX();
  }
}

In the above example, an object of the InnerClass is created using the new keyword and the outer object. The printX method of the InnerClass is called to print the value of x.

Local Inner Class Example


public class OuterClass {
  public void printMessage(final String message) {
    class InnerClass {
      public void print() {
        System.out.println(message);
      }
    }
    InnerClass inner = new InnerClass();
    inner.print();
  }

  public static void main(String[] args) {
    OuterClass outer = new OuterClass();
    outer.printMessage("Hello, world!");
  }
}

In the above example, the printMessage method creates an object of the InnerClass and calls the print method to print the message.

Anonymous Inner Class Example


public class OuterClass {
  public void printMessage() {
    Runnable runnable = new Runnable() {
      public void run() {
        System.out.println("Hello, world!");
      }
    };
    Thread thread = new Thread(runnable);
    thread.start();
  }

  public static void main(String[] args) {
    OuterClass outer = new OuterClass();
    outer.printMessage();
  }
}

In the above example, an anonymous inner class is used to create an object of the Runnable interface. The run method of the anonymous inner class is called to print "Hello, world!" to the console.

Static Nested Class Example


public class OuterClass {
  private static int x = 10;

  static class InnerClass {
    public void printX() {
      System.out.println("x = " + x);
    }
  }

  public static void main(String[] args) {
    OuterClass.InnerClass inner = new OuterClass.InnerClass();
    inner.printX();
  }
}

In the above example, an object of the InnerClass is created using the new keyword and the name of the outer class. The printX method of the InnerClass is called to print the value of x.

References

Activity