What is Inner Class in Java?
Updated on Aug 22, 2025 | 7 min read | 7.53K+ views
Share:
For working professionals
For fresh graduates
More
Updated on Aug 22, 2025 | 7 min read | 7.53K+ 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.
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:
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.
In Java, inner classes are of the following three types:
Now, let us understand each type of inner class with examples.
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.
Software Development Courses to upskill
Explore Software Development Courses for Career Progression
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:
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
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.
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
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.
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.
Yes, Java allows you to define a class within the body of another class. This is known as a nested class. This feature is used to logically group classes that are only used in one place, which increases encapsulation and leads to more readable and maintainable code. A nested class is a member of its enclosing class and, like other members, can be declared private, public, protected, or have default (package-private) access. An Inner Class in Java is a specific type of non-static nested class.
The terms are often used interchangeably, but there is a technical distinction. A nested class is any class defined within another class. There are two types: static nested classes and non-static nested classes. The term Inner Class in Java specifically refers to a non-static nested class. The key difference is that a static nested class does not have access to the instance members of its outer class, while an inner class does.
There are three main types of the non-static Inner Class in Java:
Member Inner Class: A class defined at the same level as instance variables within another class. It can access all members (including private) of the outer class.
Local Inner Class: A class defined within a method or a block of code. Its scope is restricted to that block.
Anonymous Inner Class: A local inner class that does not have a name. It is declared and instantiated in a single expression and is often used for creating one-off implementations of an interface or class, especially for event handlers.
An Inner Class in Java and a subclass serve completely different purposes. An inner class is a class that is defined within the body of another class, primarily for logical grouping and enhanced encapsulation. A subclass, on the other hand, is a class that inherits from another class using the extends keyword. An inner class has a special relationship that gives it access to the private members of its outer class, while a subclass inherits the public and protected members of its parent class.
Yes, a Java inner class can be declared as private. Just like any other member of a class (like a variable or method), making an inner class private means that it can only be accessed by the code within its enclosing outer class. This is a powerful encapsulation tool, as it allows you to create a helper class that is completely hidden from the outside world, ensuring that it is only used as intended by the outer class.
To instantiate a member Inner Class in Java, you must first have an instance of the outer class. This is because the inner class object is inherently linked to an instance of its outer class. The syntax involves using the outer class instance to create the inner class instance.
Java
// Assuming InnerClass is a member of OuterClass
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
A static nested class is a nested class that is declared with the static keyword. The most important difference between it and an Inner Class in Java (which is non-static) is that a static nested class is not associated with an instance of its outer class. This means it cannot access the non-static instance members (variables and methods) of the outer class directly. It behaves more like a regular top-level class that has just been namespaced inside another.
Since a static nested class is not tied to an instance of its outer class, you do not need an outer class object to create one. You can instantiate it directly using the name of the outer class, similar to accessing a static method.
Java
// Assuming StaticNestedClass is a static member of OuterClass
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
A local Inner Class in Java is a class that is defined inside a specific method or block of code. Its scope is limited to the block in which it is defined, meaning you cannot access or instantiate it from outside of that method. This is useful when you need a specific class for a very localized purpose, and you don't want it to be visible to the rest of the application.
A local inner class has a few key restrictions. It cannot be declared public, private, or protected, as its visibility is already limited to its enclosing block. It also cannot be static, as it is inherently tied to the method's scope. A very important rule is that a local inner class can only access final or "effectively final" local variables of the enclosing method.
An anonymous inner class is an Inner Class in Java that has no name. It is declared and instantiated at the same time, in a single expression. This is typically used to create a concise, one-time-use implementation of an interface or a subclass of a class. They are very commonly used in older Java code for event listeners in GUI applications.
Anonymous inner classes are concise but have several limitations. They cannot have a constructor because they do not have a name. They are also limited to a single expression and cannot implement multiple interfaces or extend a class and implement an interface at the same time. Because of their conciseness, they can also make code harder to read if the implementation is long.
Yes, this is one of the most powerful features of an Inner Class in Java. An inner class object has a special, implicit reference to the outer class instance that created it. This allows the inner class to directly access all members of the outer class, including its private variables and methods, which is a key reason for using them to enhance encapsulation.
In GUI programming (like with Swing or AWT), you often need to create event listener objects to respond to user actions like button clicks. An Inner Class in Java, particularly an anonymous inner class, is perfect for this. It allows you to define the listener logic right where it's being used, and since it's an inner class, it has direct access to the fields and methods of the GUI component it is a part of, making the event-handling code clean and concise.
Shadowing occurs when a variable or parameter in an inner class has the same name as a variable in its outer class. In this case, the inner class's variable "shadows" the outer class's variable. To explicitly access the outer class's variable from within the inner class, you must use the special syntax OuterClassName.this.variableName. Being aware of this is important when working with a complex Inner Class in Java.
No, a non-static Inner Class in Java cannot have static members (methods or variables), with the exception of static final constants. This is because static members belong to the class itself, not an instance, but an inner class object is always associated with an instance of the outer class. This creates a logical conflict. Static nested classes, however, can have static members.
The best way to improve is through a combination of structured learning and hands-on practice. A comprehensive program, like the software development courses offered by upGrad, can provide a strong foundation in object-oriented principles, including the nuances of the Inner Class in Java. You should also practice by building small projects, such as a simple GUI application with event listeners, to see how inner classes are used in a practical context.
You should use a static nested class when the nested class does not need to access any of the instance members of the outer class. Using static in this case is more memory-efficient, as it does not require an implicit reference to an outer class object. A good rule of thumb is to always declare a nested class as static unless it specifically needs to access the instance-level state of its enclosing class.
Yes, just as an inner class can access the private members of its outer class, the outer class can also access the private members of its inner class. However, it can only do so through an instance of the inner class. This two-way access is a unique feature of the relationship between an outer and an Inner Class in Java.
The main purpose of using an Inner Class in Java is to achieve better encapsulation and create more readable and maintainable code by logically grouping classes that are only used in one place. It allows you to define a helper class that is tightly coupled to its outer class, with special access privileges, without cluttering the public namespace of your package. It is a powerful tool for designing more elegant and organized object-oriented systems.
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
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
Top Resources