Inner classes in Java with Examples


Java supports the concept of inner classes, which allows you to define a class inside another class. Inner classes provide a way to logically group classes and enhance the encapsulation of code. In this article, we'll explore inner classes in Java, understand their types, usage, and provide detailed examples with complete Java code snippets, including main functions and output.
 

Types of Inner Classes

Java supports four types of inner classes:

 

  1. Nested Inner Class (Member Inner Class): This is the most common type of inner class. It is defined within another class and is associated with an instance of the outer class. Nested inner classes can access members of the outer class.

 

  1. Static Nested Class: Also known as a static inner class, this class is associated with the outer class itself rather than instances of the outer class. Static nested classes can be accessed without creating an instance of the outer class.

 

  1. Local Inner Class: Local inner classes are defined within a block of code, typically within a method. They have limited scope and are not accessible outside the block in which they are defined.

 

  1. Anonymous Inner Class: Anonymous inner classes are a special case of local inner classes. They are defined and instantiated at the same time, often used for implementing interfaces or extending classes in a concise manner.

 

In the following sections, we'll delve into each type of inner class with examples.

 

Nested Inner Class (Member Inner Class)

A nested inner class is defined within another class. It is associated with an instance of the outer class and can access outer class’s public and private members.

 

Example:

class Outer {
   private int outerValue = 10;

   class Inner {
       void display() {
           System.out.println("Value from outer class: " + outerValue);
       }
   }
}

public class NestedInnerClassExample {
   public static void main(String[] args) {
       Outer outer = new Outer();
       Outer.Inner inner = outer.new Inner();
       inner.display();
   }
}

 

Output:

Value from outer class: 10

 

Static Nested Class

A static nested class is associated with the outer class itself and not with instances of the outer class. It can access only static members of the outer class. Static nested class cannot access the instance variables or non-static methods of the outer class.

 

Example:

class Outer {
   static int outerValue = 10;

   static class Nested {
       void display() {
           System.out.println("Static value from outer class: " + outerValue);
       }
   }
}

public class StaticNestedClassExample {
   public static void main(String[] args) {
       Outer.Nested nested = new Outer.Nested();
       nested.display();
   }
}

 

Output:

Static value from outer class: 10

 

Local Inner Class

A local inner class is defined within a block of code, typically within a method. It has limited scope and is not accessible outside the block in which it is defined.

 

Example:

public class LocalInnerClassExample {
   void outerMethod() {
       int outerValue = 20;

       class LocalInner {
           void display() {
               System.out.println("Value from outer method: " + outerValue);
           }
       }

       LocalInner localInner = new LocalInner();
       localInner.display();
   }

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

 

Output:

Value from outer method: 20

 

Anonymous Inner Class

An anonymous inner class is a local inner class that is defined and instantiated at the same time. It is often used for implementing interfaces or extending classes in a concise manner.

 

Example:

interface Greeting {
   void greet();
}

public class AnonymousInnerClassExample {
   public static void main(String[] args) {
       Greeting greeting = new Greeting() {
           @Override
           public void greet() {
               System.out.println("Hello, Anonymous Inner Class!");
           }
       };

       greeting.greet();
   }
}

 

Output:

Hello, Anonymous Inner Class!

 

Summary

Inner classes in Java provide a way to logically group classes and enhance encapsulation. Each type of inner class has its use cases, and understanding their concepts and usage is valuable for designing clean and modular code. Whether you need to access members of the outer class or create concise implementations of interfaces or extensions, inner classes offer a flexible and powerful tool in the Java programming language.



Thanks for feedback.



Read More....
Java Exception Handling
HashMap in Java
Java : HashSet and LinkedHashSet
Java Collections Framework
Java Data Types and Variables - Declaration and Initialization