Java Inner Class

In Java, an inner class is a class that is defined within another class. It provides a way to logically group classes that are only used in one place, making your code more organized and easier to read. In this blog, we will explore Java inner classes in detail, including how to declare and use them, and provide example code snippets to illustrate each concept.

Declaring a Java Inner Class:

To declare a Java inner class, you need to declare it inside another class using the syntax below:

class OuterClass {
   ...
   class InnerClass {
      ...
   }
   ...
}

In the above example, the InnerClass is defined inside the OuterClass.

Accessing a Java Inner Class:

To access an inner class, you need to create an instance of the outer class and then create an instance of the inner class. Here is an example:

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

In the above example, we have defined an inner class called InnerClass that has a variable called y. To access the InnerClass, we need to create an instance of the OuterClass and then create an instance of the InnerClass:

OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);

In the above example, we create an instance of the OuterClass called myOuter. We then create an instance of the InnerClass called myInner using the myOuter instance. Finally, we access the variables x and y and print their sum.

Local Inner Classes:

In Java, a local inner class is a class that is defined inside a block, such as a method or an if statement. Here is an example:

class OuterClass {
   void myMethod() {
      class InnerClass {
         int x = 10;
      }
      InnerClass myInner = new InnerClass();
      System.out.println(myInner.x);
   }
}

In the above example, we have defined a local inner class called InnerClass inside the myMethod method. We then create an instance of the InnerClass and access its variable x.

Anonymous Inner Classes:

In Java, an anonymous inner class is a class that is defined and instantiated in a single expression. Here is an example:

abstract class Person {
   public abstract void eat();
}

class Demo {
   public static void main(String[] args) {
      Person myPerson = new Person() {
         public void eat() {
            System.out.println("I am eating");
         }
      };
      myPerson.eat();
   }
}

In the above example, we have defined an abstract class called Person with an abstract method called eat. We then create an anonymous inner class that extends the Person class and implements the eat method. Finally, we create an instance of the anonymous inner class and call its eat method.

Conclusion:

Java inner classes provide a way to logically group classes that are only used in one place, making your code more organized and easier to read. With the examples provided, you can get started with Java inner classes and take your programming skills to the next level.