Javatpoint Logo
Javatpoint Logo

Difference Between Nested Class and Inner Class in Java

Java, as an object-oriented programming language, provides the capability to define classes within other classes. This concept allows for better organization and encapsulation of code. Two terms that often come up in this context are nested classes and inner classes. While they are related, they serve distinct purposes in Java programming. In this section, we will explore the differences between these two concepts.

Nested Classes

A nested class is simply a class defined within another class. The outer class that contains the nested class, is referred to as the enclosing class or outer class. The key feature of a nested class is that it is entirely independent of its enclosing class. It can access all members (fields and methods) of the outer class, even if they are private.

Here is an example of a nested class:

In the above example, NestedClass is a nested class within OuterClass. It has access to the outerData field of OuterClass directly.

Inner Classes

An inner class is a specific type of nested class. It is a non-static nested class that has access to all members of the enclosing class, including those marked as private. This means that an inner class can interact with the members of the outer class as if it were a part of it.

Here is an example of an inner class:

In this example, InnerClass is an inner class. It can directly access the outerData field of OuterClass.

When to Use Nested and Inner Classes?

Use a nested class when it logically belongs to the enclosing class but does not need access to its instance variables. For example, utility classes or data structures that are specific to the enclosing class.

We can use an inner class when we need access to the instance variables of the enclosing class, or when we want to establish a close relationship between the inner and outer classes.

Let's create a complete Java program with an outer class (OuterClass), a nested class (NestedClass), and an inner class (InnerClass). We will demonstrate how they interact and access members of the enclosing class.


Data from outer class: 10
Nested class

OuterClass: It is the outer class which contains an instance variable outerData and an inner class InnerClass. It also has a static nested class NestedClass.

InnerClass: It is an inner class within OuterClass. It has a method display() that prints out the outerData.

NestedClass: It is a static nested class within OuterClass. It also has a method display() that prints out a message.

Key Differences Between Nested Classes and Inner Classes in Java

Feature Nested Class Inner Class
Access to Enclosing Class Members It can access static members of the enclosing class. It can access all members (static and instance) of the enclosing class, even if they are private.
Static vs. Non-Static It can be either static or non-static. It always non-static.
Instance Creation It can be instantiated independently of the enclosing class. It requires an instance of the enclosing class to be instantiated.
Visibility It can have any visibility (public, protected, default, private). It can have any visibility, but it is typically private to encapsulate its usage.

In Java, nested classes and inner classes provide a powerful way to organize code and improve encapsulation. Understanding the differences between them is crucial for effective usage.

Nested classes are general classes defined within another class, while inner classes are non-static nested classes with special access privileges to their enclosing class. By leveraging these features, we can design more modular and maintainable Java programs.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA