Java Inner Classes: Structuring Your Code for Better Readability

Explore Java inner classes, a feature that allows nesting classes within other classes to enhance code organization and maintainability. Learn how to access inner classes and see practical examples that demonstrate their usage in real-world applications.



Java Inner Classes

In Java, you can nest classes within other classes. This helps in grouping related classes, making your code more readable and maintainable.

Accessing Inner Class

Create an object of the outer class, then create an object of the inner class:

Example

class OuterClass {
int x = 10;

class InnerClass {
int y = 5;
}
}

public class Main {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
Output

15

Private Inner Class

An inner class can be private. This prevents outside objects from accessing it:

Example

class OuterClass {
int x = 10;

private class InnerClass {
int y = 5;
}
}

public class Main {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();  // Error
System.out.println(myInner.y + myOuter.x);
}
}
Output

Main.java:13: error: OuterClass.InnerClass has private access in OuterClass
OuterClass.InnerClass myInner = myOuter.new InnerClass();
                ^

Static Inner Class

A static inner class can be accessed without creating an object of the outer class:

Example

class OuterClass {
int x = 10;

static class InnerClass {
int y = 5;
}
}

public class Main {
public static void main(String[] args) {
OuterClass.InnerClass myInner = new OuterClass.InnerClass();
System.out.println(myInner.y);
}
}
Output

5

Note: A static inner class does not have access to the members of the outer class.

Access Outer Class From Inner Class

Inner classes can access attributes and methods of the outer class:

Example

class OuterClass {
int x = 10;

class InnerClass {
public int myInnerMethod() {
    return x;
}
}
}

public class Main {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.myInnerMethod());
}
}
Output

10
Advantage of Java Inner Classes

There are three advantages of inner classes in Java:

  • Access to all members of the outer class, including private.
  • More readable and maintainable code by grouping classes and interfaces.
  • Code Optimization: Less code to write.
Need for Java Inner Class

Sometimes, a class should not be accessed by any other class. Nesting it inside another class helps achieve this.

If all objects of a class are part of the outer object, it is easier to nest it within the outer class.

Types of Nested Classes

There are two types of nested classes: non-static and static.

Non-static nested classes are also known as inner classes:

  • Member Inner Class
  • Anonymous Inner Class
  • Local Inner Class

Static nested classes:

  • Static Nested Class
Types of Inner Classes
Type Description
Member Inner Class A class created within a class and outside a method.
Anonymous Inner Class A class for implementing an interface or extending a class. The compiler decides its name.
Local Inner Class A class created within a method.
Static Nested Class A static class created within a class.
Nested Interface An interface created within a class or interface.