Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

What Is An Inner Class In Java?

An inner class is a class defined within another class, known as the outer class. Inner classes are useful when you want to group related classes together, or when you want to access private members of the outer class. In this tutorial, we'll discuss the basics of inner classes in Java, including the different types of inner classes and their use cases.

  • Non-static Nested Class (Inner Class)

A non-static nested class is an inner class that is not declared as static. Instances of a non-static nested class have access to the members (including private members) of the outer class.

Example:

OuterClass.java:

public class OuterClass {
    private String message = "Hello, World!";

    class InnerClass {
        public void displayMessage() {
            System.out.println("Message from outer class: " + message);
        }
    }
}

Main.java:

public class Main {
    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();
        inner.displayMessage(); // Message from outer class: Hello, World!
    }
}

In this example, we create an outer class called OuterClass with a private member message. We then define an inner class called InnerClass within the outer class. The InnerClass has access to the message member of the outer class, even though it is private.

  • Static Nested Class

A static nested class is an inner class that is declared as static. Instances of a static nested class do not have direct access to the non-static members of the outer class, and they can be instantiated without creating an instance of the outer class.

Example:

OuterClass.java:

public class OuterClass {
    private static String message = "Hello, World!";

    static class StaticNestedClass {
        public void displayMessage() {
            System.out.println("Message from outer class: " + message);
        }
    }
}

Main.java:

public class Main {
    public static void main(String[] args) {
        OuterClass.StaticNestedClass nested = new OuterClass.StaticNestedClass();
        nested.displayMessage(); // Message from outer class: Hello, World!
    }
}

In this example, we define a static nested class called StaticNestedClass within the OuterClass. The StaticNestedClass can be instantiated directly without creating an instance of the OuterClass. Note that the message member of the outer class must also be declared as static for it to be accessible from the static nested class.

  • Local Inner Class

A local inner class is an inner class defined within a method or a scope block of the outer class. Local inner classes have access to the final variables and parameters of the enclosing block.

Example:

OuterClass.java:

public class OuterClass {
    public void createLocalInnerClass() {
        final String message = "Hello, World!";

        class LocalInnerClass {
            public void displayMessage() {
                System.out.println("Message from outer class method: " + message);
            }
        }

        LocalInnerClass localInner = new LocalInnerClass();
        localInner.displayMessage();
    }
}

Main.java:

public class Main {
    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        outer.createLocalInnerClass(); // Message from outer class method: Hello, World!
    }
}

In this example, we define a local inner class called LocalInnerClass within the createLocalInnerClass method of the OuterClass.

  1. Creating and using Inner Classes in Java

    // Outer class
    class Outer {
        // Member Inner Class
        class Inner {
            void display() {
                System.out.println("Inner class method");
            }
        }
    }
    
    // Usage
    Outer outer = new Outer();
    Outer.Inner inner = outer.new Inner();
    inner.display();
    
  2. Anonymous Inner Classes in Java

    // Interface
    interface Greeting {
        void greet();
    }
    
    // Anonymous Inner Class implementing the interface
    Greeting anonymousGreeting = new Greeting() {
        @Override
        public void greet() {
            System.out.println("Hello from anonymous class");
        }
    };
    
    anonymousGreeting.greet();
    
  3. Accessing outer class members in Inner Classes

    class Outer {
        private int outerVar = 10;
    
        class Inner {
            void displayOuterVar() {
                System.out.println("Outer variable: " + outerVar);
            }
        }
    }
    
  4. Static Inner Classes in Java

    class Outer {
        static class StaticInner {
            void display() {
                System.out.println("Static Inner class method");
            }
        }
    }
    
    // Usage
    Outer.StaticInner staticInner = new Outer.StaticInner();
    staticInner.display();
    
  5. Local Inner Classes and their scope in Java

    class Outer {
        void outerMethod() {
            // Local Inner Class
            class LocalInner {
                void display() {
                    System.out.println("Local Inner class method");
                }
            }
    
            LocalInner localInner = new LocalInner();
            localInner.display();
        }
    }