Inner Class in Java

Last Updated : 21 Nov, 2025

An inner class is a class declared inside the body of another class. The inner class has access to all members (including private) of the outer class, but the outer class can access the inner class members only through an object of the inner class.

Syntax:

class OuterClass {
// Outer class members

class InnerClass {
// Inner class members
}

Example:

Java
public class OuterClass {

    class InnerClass {
        void display() {
            System.out.println("Hello from Inner Class!");
        }
    }

    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        InnerClass inner = outer.new InnerClass();
        inner.display();
    }
}

Output
Hello from Inner Class!

Features of Inner Classes

  • Encapsulation: Inner classes can access private members of the outer class, providing better encapsulation.
  • Code Organization: Logically groups classes that belong together, making code more readable.
  • Access to Outer Class: Inner class instances have a reference to the outer class instance.
  • Namespace Management: Helps avoid naming conflicts by nesting related classes.

Types of Inner Classes

Java supports four types of inner classes:

  1. Member Inner Class
  2. Method-Local Inner Class
  3. Static Nested Class
  4. Anonymous Inner Class
inner_classes_in_java
Inner Class in Java

1. Member Inner Class

A member inner class is a non-static class defined at the member level of another class. It has access to all members of the outer class, including private members.

Java
class Outer {
    private int outerVar = 100;
    
    // Member inner class
    class Inner {
        void display() {
            System.out.println("Outer variable: " + outerVar);
        }
    }
}

class Main {
    public static void main(String[] args) {
        // Creating inner class instance
        Outer.Inner inner = new Outer().new Inner();
        inner.display();
    }
}

Output
Outer variable: 100
  • Cannot have static members (except static final constants) until Java 16.
  • Must be instantiated with an outer class instance.
  • Syntax: Outer.Inner inner = outer.new Inner();

Before Java 16: member inner classes could NOT have static members (except static final constants).

After Java 16: Member inner classes CAN have static member, Only if they do not depend on an outer class instance

2. Method-Local Inner Class

A method-local inner class is defined inside a method of the outer class. It can only be instantiated within the method where it is defined.

Java
class Outer {
    void outerMethod() {
        System.out.println("Inside outerMethod");
        
        // Method-local inner class
        class Inner {
            void innerMethod() {
                System.out.println("Inside innerMethod");
            }
        }
        
        Inner inner = new Inner();
        inner.innerMethod();
    }
}

class Main {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.outerMethod();
    }
}

Output
Inside outerMethod
Inside innerMethod

Accessing Local Variables:

From Java 8 onwards, method-local inner classes can access effectively final or final local variables.

Java
class Outer {
    void outerMethod() {
        int x = 98; // Effectively final
        System.out.println("Inside outerMethod");
        
        class Inner {
            void innerMethod() {
                System.out.println("x = " + x);
            }
        }
        
        Inner inner = new Inner();
        inner.innerMethod();
    }
}

class Main {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.outerMethod();
    }
}

Output
inside outerMethod
x= 98
  • Cannot access non-final local variables before Java 8.
  • From Java 8 onwards, can access effectively final local variables.
  • Cannot be declared as private, protected, static, or transient.
  • Can be declared as abstract or final, but not both.

3. Static Nested Classes

A static nested class is a static class defined inside another class. It does not have access to instance members of the outer class but can access static members.

Java
class Outer {
    private static int staticVar = 50;
    
    // Static nested class
    static class Inner {
        void display() {
            System.out.println("Static variable: " + staticVar);
        }
    }
}

class Main {
    public static void main(String[] args) {
        // No need for outer class instance
        Outer.Inner inner = new Outer.Inner();
        inner.display();
    }
}

Output
Static variable: 50

4. Anonymous Inner Classes 

An anonymous inner class is an inner class without a name. It is declared and instantiated in a single statement. It is used to provide a specific implementation of a class or interface on the fly.

Types of Anonymous Inner Classes:

a) As a Subclass

Java
class Demo {
    void show() {
        System.out.println("Inside Demo's show method");
    }
}

class Main {
    public static void main(String[] args) {
        // Anonymous inner class extending Demo
        Demo obj = new Demo() {
            @Override
            void show() {
                super.show();
                System.out.println("Inside Anonymous class");
            }
        };
        
        obj.show();
    }
}

Output
Inside Demo's show method
Inside Anonymous class

b) As an Interface Implementation

Java
interface Hello {
    void greet();
}

class Main {
    public static void main(String[] args) {
        // Anonymous inner class implementing Hello
        Hello hello = new Hello() {
            @Override
            public void greet() {
                System.out.println("Hello from Anonymous class");
            }
        };
        
        hello.greet();
    }
}
  • Declared and instantiated in one statement.
  • Used for one-time use implementations.
  • Cannot have constructors (since it has no name).
  • Commonly used in event handling and functional programming.

Complete Implementation Example

Java
public class OuterClass {
    private int outerVar;
    
    public OuterClass(int var) {
        this.outerVar = var;
    }
    
    public void outerMethod() {
        System.out.println("This is an outer method");
    }
    
    // Member inner class
    public class InnerClass {
        private int innerVar;
        
        public InnerClass(int var) {
            this.innerVar = var;
        }
        
        public void innerMethod() {
            System.out.println("This is an inner method");
            System.out.println("Inner variable: " + innerVar);
        }
        
        public void accessOuterVar() {
            System.out.println("Outer variable from inner class: " + outerVar);
        }
    }
    
    public static void main(String[] args) {
        // Create outer class instance
        OuterClass outer = new OuterClass(10);
        
        // Create inner class instance
        OuterClass.InnerClass inner = outer.new InnerClass(20);
        
        // Call inner class methods
        inner.innerMethod();
        inner.accessOuterVar();
    }
}

Output
This is an inner method
Inner variable: 20
Outer variable from inner class: 10
Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment