Nested and Inner Classes in Java


In Java, a class can be defined within another class. These are known as nested classes. Nested classes are used to logically group classes that are only used in one place, which makes the code more readable and maintainable. There are two types of nested classes in Java:

  • Static Nested Classes: A nested class that is declared static.
  • Inner Classes: A nested class that is non-static and has access to the instance variables and methods of the outer class.

1. Static Nested Class

A static nested class is a nested class that is declared static. It can access only the static members (variables and methods) of the outer class. It cannot access instance variables or methods of the outer class directly.

Syntax of Static Nested Class

    class OuterClass {
        static class NestedClass {
            // Code of the nested class
        }
    }
        

Example 1: Static Nested Class

In this example, we define a static nested class Engine inside the outer class Car. The nested class can access the static members of the outer class, but not its instance variables.

    class Car {
        static String brand = "Toyota"; // Static variable in outer class
    
        static class Engine {
            void displayBrand() {
                System.out.println("Car brand is: " + brand); // Accessing static member of outer class
            }
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Car.Engine engine = new Car.Engine(); // Creating instance of static nested class
            engine.displayBrand(); // Calling method of static nested class
        }
    }
        

Output:

    Car brand is: Toyota
        

Explanation:

  • The class Engine is declared as a static nested class inside the outer class Car.
  • The static nested class can access the static variable brand of the outer class Car.
  • The instance of the static nested class is created using the outer class name: Car.Engine engine = new Car.Engine();.

2. Inner Classes

An inner class is a non-static nested class that can access both the static and instance members of the outer class. It is associated with an instance of the outer class, so to create an inner class, we first need an instance of the outer class.

Syntax of Inner Class

    class OuterClass {
        class InnerClass {
            // Code of the inner class
        }
    }
        

Example 2: Inner Class

In this example, the inner class Wheel has access to the instance variable brand of the outer class Car.

    class Car {
        String brand = "Toyota"; // Instance variable in outer class
    
        class Wheel {
            void displayBrand() {
                System.out.println("Car brand is: " + brand); // Accessing instance member of outer class
            }
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Car car = new Car(); // Creating instance of outer class
            Car.Wheel wheel = car.new Wheel(); // Creating instance of inner class
            wheel.displayBrand(); // Calling method of inner class
        }
    }
        

Output:

    Car brand is: Toyota
        

Explanation:

  • The inner class Wheel can access the instance variable brand of the outer class Car.
  • To create an instance of the inner class, we need an instance of the outer class: Car car = new Car();.
  • The inner class instance is created using: Car.Wheel wheel = car.new Wheel();.

3. Local Inner Class

A local inner class is a class that is defined within a method of the outer class. It is local to the method and can only be used inside the method where it is defined.

Example 3: Local Inner Class

In this example, the local inner class Engine is defined inside the startEngine method of the outer class Car.

    class Car {
        void startEngine() {
            class Engine { // Local inner class
                void displayMessage() {
                    System.out.println("Engine started!");
                }
            }
    
            Engine engine = new Engine(); // Creating instance of local inner class
            engine.displayMessage(); // Calling method of local inner class
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Car car = new Car(); // Creating instance of outer class
            car.startEngine(); // Calling method that contains local inner class
        }
    }
        

Output:

    Engine started!
        

Explanation:

  • The inner class Engine is defined inside the startEngine method of the outer class Car.
  • The local inner class can only be used within the method where it is defined.
  • To create an instance of the local inner class, we do so inside the method: Engine engine = new Engine();.

4. Anonymous Inner Class

An anonymous inner class is a local inner class without a name. It is used to instantiate a class and define its behavior in a single expression.

Example 4: Anonymous Inner Class

In this example, an anonymous inner class is used to implement the Greeting interface and override its greet method.

    interface Greeting {
        void greet();
    }
    
    public class Main {
        public static void main(String[] args) {
            Greeting greeting = new Greeting() { // Anonymous inner class
                public void greet() {
                    System.out.println("Hello, welcome!");
                }
            };
    
            greeting.greet(); // Calling method of anonymous inner class
        }
    }
        

Output:

    Hello, welcome!
        

Explanation:

  • In this example, an anonymous inner class is used to implement the Greeting interface.
  • The anonymous inner class overrides the greet method and defines its behavior.
  • We do not need to explicitly declare a class for the inner class, and it is instantiated in a single line.

5. Conclusion

Nested and inner classes are powerful features in Java that allow you to define classes within other classes. They are useful for logically grouping related classes, improving code readability, and encapsulating functionality. There are various types of nested and inner classes, such as static nested classes, inner classes, local inner classes, and anonymous inner classes, each serving different purposes in Java programming.





Advertisement