What is Inner Class in Java?

By Pavan Vadapalli

Updated on Aug 22, 2025 | 7 min read | 7.46K+ views

Share:

Think of a car. It's an object. Now, think of its engine. The engine is a complex object in its own right, but it only exists to serve the car. It doesn't make sense to have a car engine sitting by itself. This is the core idea behind an Inner Class in Java.  

An inner class is a "helper" class that is defined inside another class, allowing you to logically group functionality that is only used in one place. This powerful feature enhances encapsulation and makes your code more organized and readable. 

This blog we will deep dive into the world of the Inner Class in Java, exploring its different types and showing you how to use them effectively in your own programs.

Mastering concepts like the switch case in Java is just the beginning. Ready to build powerful, real-world applications? Explore our Online Software Engineering Courses to gain expert-level skills in core Java, data structures, and industry best practices. Launch your developer career today!

Meaning of Inner Class in Java

A non-static nested class or inner class is defined within another class and has access to members of the outer class (enclosing class). On the contrary, a static nested class is also a class defined within another class, but unlike a non-static nested class, it cannot access the member variables of its outer class. In the case of a non-static nested class, we are not required to create an instance of the outer class. But in the case of an inner class, we must first instantiate the outer class to be able to instantiate the inner class.

If you're looking to master core Java concepts like classes and objects in Java and build a strong foundation in programming, here are some top-rated courses from upGrad to help you get there: 

Source

Learn Online software development courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Types of Inner Class in Java 

In Java, inner classes are of the following three types:

  • Member inner class
  • Anonymous inner class
  • Local inner class

Now, let us understand each type of inner class with examples. 

1. Member Inner Class

A member inner class in Java is a non-static nested class created inside a class but outside a method. Also referred to as a regular inner class, a member inner class can be declared with access modifiers like private, public, default, and protected.

The syntax for a member inner class is as follows:

class Outer{  

 //code  

 class Inner{  

  //code  

 }  

}  

Member inner class in Java example:

Given below is a program to demonstrate how to create an inner class and access it. We will make the inner class private and use the method display_Inner()to access the class.

class DemoOuter {

   int num;

 // inner class

   private class DemoInner {

      public void print() {

         System.out.println(“This is the inner class”);

      }

   }

   // Accessing the inner class from the method within

   void display_Inner() {

      DemoInner inner = new DemoInner();

      inner.print();

   }

}

   public class MyClass {

   public static void main(String args[]) {

      // Instantiating the outer class 

      DemoOuter outer = new DemoOuter();

      

      // Accessing the display_Inner() method

      outer.display_Inner();

   }

}

Output: This is the inner class

In the above example, OuterDemo is the outer class, InnerDemo is the inner class, we are instantiating the inner class inside the method display_Inner(), which is invoked from the main method.

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

2. Anonymous Inner Class

An anonymous inner class in Java is an inner class declared without any class name and for which only a single object is created. Anonymous inner classes in Java are declared and instantiated at the same time. They are most valuable when we want to override the method of a class or interface. 

The syntax for an anonymous inner class in Java is as follows:

AnonymousInner an_inner = new AnonymousInner() {

   public void my_method() {

      

   //code

   }   

};

 An anonymous inner class in Java can be created in two ways:

  1. Using class
  2. Using interface

Anonymous inner class in Java example (using class):

abstract class Meal{  

  abstract void eat();  

}  

class TestAnonymousInner{  

 public static void main(String args[]){  

  Meal m=new Meal(){  

  void eat(){System.out.println(“Bon appetit!”);}  

  };  

  m.eat();  

 }  

}  

Output: Bon appetit!

Anonymous inner class in Java example (using interface):

interface Game{  

 void play();  

}  

class TestAnnonymousInner1{  

 public static void main(String args[]){  

 Game g=new Game(){  

  public void play(){System.out.println(“Outdoor games”);}  

 };  

 g.play();  

 }  

}  

Output: Outdoor games

3. Local Inner Class

A local inner class in Java is a class created inside a method. As in the case of local variables, the scope of the local inner class remains restricted within the method. 

Local inner classes are defined inside a block: a method body, an if clause, or a for loop. A local inner class is not a member of any enclosing class, but instead, it belongs to the block within which it is defined. Thus, even though a local inner class in Java cannot have any access modifiers associated with it, you can mark it abstract or final. If we want to invoke the methods of a local inner class, we have to instantiate the class inside the method.

Local inner class in Java example:

Below is an example program to demonstrate how to instantiate the object of a local inner class declared inside the method of an outer class.

package innerClass; 

public class Outer_Class 

{ 

// An instance method of class Outer_Class. 

   public void display()

  { 

// Declaration of a method local inner class. 

    class Inner_Class 

    { 

       public void msg(){ 

        System.out.println(“This is a local inner class!”); 

       } 

     } 

// Create an instance of a method local inner class and call the msg() method using object reference variable i. 

     Inner_Class i = new Inner_Class(); 

       i.msg(); 

  } 

public static void main(String[] args) 

{ 

// Create an object of the outer class Outer_Class. 

     Outer_Class o = new Outer_Class(); 

       o.display(); 

   } 

}

Output: This is a local inner class!

With the fundamentals of Java inner class in mind, let us understand why we need them in the first place.

Advantages of Java Inner Class

An inner class in Java brings three significant utilities to the table. These are as follows:

1. Since a Java inner class can access the data members and methods of the outer/main class, including private, it represents a specific type of relationship.

2. A Java inner class significantly reduces coding since it requires less to write.

3. An inner class in Java logically groups classes and interfaces in a single place, thereby enhancing the readability of the code and making it easier to maintain.

Subscribe to upGrad's Newsletter

Join thousands of learners who receive useful tips

Promise we won't spam!

Summing Up

The key takeaway is that an Inner Class in Java is a powerful tool for creating more organized, readable, and encapsulated code. As we've seen, it's the perfect solution when you have a helper class that is tightly coupled to another, or when you need to handle events in GUI programming. 

Mastering when and how to use an Inner Class in Java is not just about understanding a niche feature; it's about embracing a more sophisticated approach to object-oriented design that leads to cleaner and more maintainable applications. 

Wondering where to learn Java?

AI-Powered Full Stack Development Course by IIITB is what you’re looking for! 

The AI-Powered Full Stack Development Course by IIIT Bangalore is a 9.5-month online program designed to provide learners with expertise in software development, combining frontend and backend technologies with AI integration.  

This program is structured for fresh graduates, working professionals, and tech enthusiasts who want to gain hands-on experience in AI-powered web applications. 

Sign up today to learn from the best in the industry!

Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.

Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.

Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.

Frequently Asked Questions (FAQs)

1. Can we write a class inside a class in Java?

2. What is the difference between a nested class and an inner class?

3. What are the different types of inner classes in Java?

4. What is the difference between an inner class and a subclass?

5. Can a Java inner class be private? 

6. How do you instantiate a member inner class?

7. What is a static nested class, and how does it differ from an inner class?

8. How do you instantiate a static nested class?

9. What is a local inner class?

10. What are the restrictions on a local inner class?

11. What is an anonymous inner class?

12. What are the limitations of an anonymous inner class?

13. Can an inner class access the private members of its outer class?

14. Why would you use an inner class for event handling in a GUI?

15. What is a "shadowing" problem with inner classes?

16. Can an inner class have static members?

17. How can I improve my understanding of inner classes?

18. When should I use a static nested class instead of an inner class?

19. Can an outer class access the private members of its inner class?

20. What is the main purpose of using an Inner Class in Java?

Pavan Vadapalli

900 articles published

Pavan Vadapalli is the Director of Engineering , bringing over 18 years of experience in software engineering, technology leadership, and startup innovation. Holding a B.Tech and an MBA from the India...

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

upGrad

AI-Driven Full-Stack Development

Job-Linked Program

Bootcamp

36 Weeks

upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months

IIIT Bangalore logo
new course

Executive PG Certification

9.5 Months