VOOZH about

URL: https://www.geeksforgeeks.org/java/inner-class-java/

⇱ Inner Class in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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:


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.


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.


Output
Inside outerMethod
Inside innerMethod

Accessing Local Variables:

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


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.


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


Output
Inside Demo's show method
Inside Anonymous class

b) As an Interface Implementation

  • 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


Output
This is an inner method
Inner variable: 20
Outer variable from inner class: 10
Comment
Article Tags:
Article Tags: