Inner Classes in Java

Inner Classes in Java with Examples

In this article, I am going to discuss Inner Classes in Java with examples. Please read our previous article, where we discussed Constructors in Java. At the end of this article, you will understand what are inner classes and their type and when and how to implement this in Java Applications.

What are Java Inner Classes?

The inner class is defined inside the body of another class (known as an outer class). The class written within is called the nested class, and the class that holds the inner class is called the outer class. Java inner class can be declared private, public, protected, or with default access whereas an outer class can have only public or default access.

We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable. The Syntax is given below.

Inner Classes in Java

Types of Inner Classes in Java:

The inner classes are of four types. They are :

  1. Nested Inner Class
  2. Method Local Inner Class
  3. Anonymous Inner Class
  4. Static Nested Class

Types of Inner Classes in Java

Nested Inner Class in Java:

A class created within the class and outside the method is known as Nested Inner Class. It can access the private instance variable of the outer class.

Sample Program for Nested Inner Class:
package Demo;

public class NestedInnerClass {
 class Inner { 
       public void show() { 
            System.out.println("In a nested class method"); 
 public static void main(String[] args) { 
  NestedInnerClass.Inner in = new NestedInnerClass().new Inner();; 

Output: In a nested class method

Method Local Inner Class in Java:

A class created within the method of the enclosing class is known as Method Local Inner Class. Since the local inner class is not associated with Object, we can’t use private, public, or protected access modifiers with it. The only allowed modifiers are abstract or final.

Sample Program for Method Local Inner Class:
package Demo;

public class MethodLocalInnerClass {
 void outerMethod() { 
        System.out.println("Inside OuterMethod"); 
        // Inner class is local to outerMethod() 
        class Inner { 
            void innerMethod() { 
                System.out.println("Inside InnerMethod"); 
        Inner y = new Inner(); 
 public static void main(String[] args) { 
  MethodLocalInnerClass outer = new MethodLocalInnerClass(); 

Inside OuterMethod
Inside InnerMethod

Anonymous Inner Class in Java:

An inner class declared without a class name is known as an anonymous inner class. It is created for implementing an interface or extending class. Since an anonymous class has no name, it is not possible to define a constructor for an anonymous class. Its name is decided by the java compiler.

Sample Program for Anonymous Inner Class:
package Demo;
abstract class Animal{  
  abstract void dog();  
class AnonymousInnerClass{  
 public static void main(String args[]){  
  Animal p=new Animal(){  
  void dog(){
   System.out.println("Dog is an Animal.");

Output: Dog is an Animal.

Static Nested Class:

Static nested classes are not technically inner classes. They are like static members of the outer class. A static nested class is the same as any other top-level class and is nested for only packaging convenience. Because this is static in nature so this type of inner class doesn’t share any special kind of relationship with an instance of the outer class. A static nested class cannot access non-static members of the outer class.

Sample Program for Static Nested Class:
package Demo;

public class StaticNestedClass {
    static class Nested_Demo {
        public void my_method() {
           System.out.println("This is my nested class");
     public static void main(String args[]) {
      StaticNestedClass.Nested_Demo nested = new StaticNestedClass.Nested_Demo();	 

Output: This is my nested class

Advantages of Inner Classes in Java:
  • It helps in Code Optimization.
  • If a class is useful to only one class, it makes sense to keep it nested and together. It helps in the packaging of the classes.
  • It has nested classes that are used to develop a more readable and maintainable code.
  • The inner classes can access outer class private members and at the same time, we can hide the inner class from the outer world.
  • It requires less code to write.

In the next article, I am going to discuss Java Wrapper Classes in detail. Here, in this article, I try to explain the Inner Classes in Java with examples and I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *