Java

Inner Class in Java | Explained

Java provides a feature that allows us to create a class within another class known as the nested class or inner class and the class that holds the inner class is referred as an outer class. The inner class can be specified in the same way as we specify any other class attribute or method.

In java, we can’t declare/create a class with an access modifier private so that the class can be accessed by any other class from inside or outside the package. However, to provide security, we need to create private classes, and to do so, we can create inner classes with private access modifiers.

This write-up will provide a comprehensive guide for inner classes in Java and in this regard, we need to understand the following concepts:

Let’s get started!

What is Inner Class in Java

A class that is declared/created within some other class is known as an inner class. It can access all the class attributes and member functions of the outer class. The primary features of inner classes include enhanced security, code readability, and maintainability.

Syntax

The basic syntax of the inner class is described in the below-given snippet:

class OuterClass{  
 //Statements
 class InnerClass{  
  //Statements
 }  
}

How to create and access Inner Class in Java

Inner class creation is very simple, all we have to do is create a class within a class. In order to access the inner class, first, we have to create an instance/object of the outer class and afterward, we can create the object of the inner/nested class.

Example

The above snippet shows how an inner class works in Java.

How to Access Attributes/Methods of Outer Class

The inner class can access the class attributes or member functions of the outer class, the following example will provide a better understanding:

Example

The below-given code snippet access the name1 attribute of the Outer class from the inner class:

class Outer {

    String name1 = "John";

    class Inner {

        public String name() {
            return name1;
        }
    }
}

public class MainClass {

    public static void main(String[] args) {
        Outer obj1 = new Outer();
        Outer.Inner obj2 = obj1.new Inner();
        System.out.println(obj2.name());
    }

}

The complete code and its output is shown in the below-given snippet:

The output authenticates that the inner class successfully accesses the class attribute of the outer class.

Private Inner Class in Java

In Java, a regular class can’t be created with the private access modifier however an inner class can be created with the private access and the objects outside the class can’t access the private inner class.

Example

If someone tries to access the private inner class, then java will throw an error:

class Outer {
  String name1 = "John";


  private class Inner {
      String name2 = "Micheal";
    }
}
public class MainClass {

  public static void main(String[] args) {
     Outer obj1 = new Outer();
     Outer.Inner obj2 = obj1.new Inner();
     System.out.println(obj2.name2 + " " + obj1.name1);
    }
}

The entire code is same except the inner class access modifier:

The above snippet verifies that an outer class is not able to access the inner class.

Static Inner Class in Java

An inner class can also be created with the static access modifier and the benefit of static inner class is that there is no need to create the object of the outer class for the creation of object of static inner class.

The instance/object of the static class can be created using the following syntax:

OuterClass.InnerClass obj = new OuterClass.InnerClass();

Example

class Outer {

    String name1 = "John";

    static class Inner {

        String name2 = "Micheal";
    }
}

public class MainClass {

    public static void main(String[] args) {
        Outer.Inner obj2 = new Outer.Inner();
        System.out.println(obj2.name2);
    }

}

The complete code and respective output will go like this:

A static inner class is not capable to access the class attributes or methods of the outer class:

The above code snippet verifies that static inner class can’t access the member of outer class.

Conclusion

In Java a class can be created within another class referred as inner class and to create an inner class first we have to create an instance/object of the outer class and then we can create the object of the inner class. A private inner class can also be created in java and it can’t be accessed from the outer class. Moreover, in a static inner class, there is no need to create the object of the outer class to access the inner static class and it can’t access the class members of the outer class.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.