Java Lesson 29 – Inner Classes | Dataplexa

Inner Classes

An inner class in Java is a class that is defined inside another class. Inner classes are used to logically group classes that are closely related and to improve code organization and readability.

In real-world Java applications, inner classes are often used to represent helper logic that is tightly coupled with an outer class.


Real-World Understanding of Inner Classes

Think of a car. A car has an engine, dashboard, and steering system. These components exist only because the car exists.

Similarly, an inner class exists only in the context of its outer class. It does not make sense on its own.


Why Java Supports Inner Classes

Inner classes help developers:

  • Group related functionality together
  • Improve code readability
  • Access outer class members easily
  • Reduce unnecessary exposure of helper classes

They are commonly used in frameworks, event handling, and UI-based applications.


Types of Inner Classes in Java

Java supports different types of inner classes:

  • Non-static (Member) Inner Classes
  • Static Nested Classes
  • Local Inner Classes
  • Anonymous Inner Classes

In this lesson, we focus on the most commonly used ones.


Member Inner Class

A member inner class is defined inside a class but outside any method. It can access all members of the outer class.


class Car {

    private String model = "Sedan";

    class Engine {
        void showDetails() {
            System.out.println("Engine belongs to " + model);
        }
    }
}

Here, the inner class Engine can access the private member model.


Using a Member Inner Class


public class Main {
    public static void main(String[] args) {

        Car car = new Car();
        Car.Engine engine = car.new Engine();
        engine.showDetails();
    }
}

The inner class object is created using the outer class object.


Static Nested Class

A static nested class belongs to the outer class, but it cannot directly access non-static members of the outer class.


class University {

    static String name = "Dataplexa University";

    static class Department {
        void showDepartment() {
            System.out.println("Department of Computer Science");
        }
    }
}

Static nested classes are often used for grouping utility logic.


Using a Static Nested Class


public class Main {
    public static void main(String[] args) {

        University.Department dept = new University.Department();
        dept.showDepartment();
    }
}

Notice that no outer class object is required.


Anonymous Inner Class (Concept)

An anonymous inner class is used when a class is needed only once. It does not have a name and is often used for event handling or callbacks.


Runnable task = new Runnable() {
    public void run() {
        System.out.println("Task running");
    }
};

Anonymous inner classes are common in older Java code and are still important to understand.


When to Use Inner Classes

Inner classes are useful when:

  • A class is tightly coupled to another class
  • Logic should not be exposed publicly
  • Grouping improves readability

However, overusing inner classes can make code harder to maintain.


Key Takeaways

  • Inner classes are defined inside another class
  • They help group related logic
  • They can access outer class members
  • They are widely used in real Java applications

In the next lesson, we will build a small OOP-based mini project to apply everything we have learned so far.