Inner Class in Java | Uses, Realtime Example

So far we have seen the members of the class are variables, methods, constructors, and initializer blocks. But it is also possible to have a class or an interface as a member of a class.


A class declared inside another class is known as nested class. The class which is a member of another class can be either static or non-static.

The member class which is declared as static is known as static nested class. The member class which is non-static is known as an inner class or non-static nested class


In this tutorial, we will learn only the basic ideas of inner class in Java and later on detail will be discussed in the further tutorial.
Inner class in Java with realtime example

Inner class in Java

An inner class in Java is a class that is declared inside of another Java class without static modifier keyword. It is also commonly known as a non-static nested class in Java. 

An inner class cannot have any kind of static member. So, the members of an inner class can be:
1. Instance variables
2. Instance methods
3. Constructors
4. Initializer block
5. Inner class

Syntax of Inner class:
access modifier class OuterClassName {
         . . . . . . . . . .
       access modifier class InnerClassName  {
            // Members of the inner class.
  // Other members of the outer class.
The class that holds the inner class is called enclosing or outer class. The enclosing class is a top-level class whereas the inner class is the member of outer class.

The relationship between the outer class and the inner class is Has-A relationship. The Has-A relationship is also known as composition or aggregation in Java.

When an inner class is the enclosing class for another inner class, it is called nesting of inner class. There is no limit of nesting of inner classes.

In which version Inner classes were added in Java?

Inner classes were added in the Java 1.1 version without any changes in the JVM used to handle the class files. Inner classes are implemented fully with the help of the compiler. the compiler generates a separate dot class file for each inner class named Outer$Inner.class and for an outer class named Outer.class

If any dot class file name contains $ symbol in its middle, it shows the inner class file. The format of the class files for inner classes have the same as class files for the outer classes. Therefore, JVM treats the class files for inner classes and the outer classes the same.

Properties of Inner class in Java

1. The inner class can have the same name as the outer class.
2. The scope of inner class is bounded by the scope of its outer class.
3. Without existing an outer class object or instance, there will be no chance of existing inner class object.
3. An inner class can directly access all the variables and methods of the outer class including private.

4. Since the inner class is a regular member of the outer class just like declared variables and methods in the class. That’s why we can apply all the access modifiers such as public, default, protected, and private to the inner classes similar to the other members of a class. But the outer or normal class cannot be private or protected.

5. If the variable name of inner class is the same as the variable name of outer class, we can access the outer class variable like this.
    OuterClassName.this.VariableName; here this represents current outer class object.

Instantiating an inner class

The instances of an inner class share a relationship with instances of an outer class. This relationship is such that we cannot create an instance of the inner class without having an instance of the outer class.

Therefore, to create an instance of inner class, you must have to create an instance of outer class to tie to the inner class. We can create multiple instances of the inner class for the same instance of an outer class.

Let’s take a real-time example to understand this concept.

Real-time Example of Inner class in Java

First Real-time Example:
Suppose there is a university. University contains several departments such as electronics, computer science, electrical, mechanical etc. Assume that tomorrow, Government decided to close this university due to its illegal activity happening in the university. If the university is closed, all departments of this university will be closed automatically.

That’s why we can say that departments are tied to their university. i.e their functionalities are tied with its university. So, without an existing university object, there is no chance of existing department objects. Since the department is always a part of the university class. Hence, we must declare the department class inside the university class.
   class University {  // University is outer class.
         . . . . . . .
        class Department { // Department is inner class.
          . . . . . . . . . . . . 
Thus, without an existing one type of object, if there is no chance of existing another type of object, we should go for inner class.

Second Real-time Example:
Assume that there is a car. Within a car, there are several important individual components that are required. The most important component of the car is the engine. The engine must be required to make a car. But without an existing car object, there is also no chance of existing an engine object.

Since an engine is always an important part of a car. Hence, the class engine must be declared inside the class car. 
   class Car {
       . . . . . . . . .
      class Engine  {
        . . . . . . . . . . 
The above two real-time examples are explained to get the basic ideas of inner class. Now, the next example is Java API.

Third Real-time Example:
As you know that in Java there is a Map concept in the collections API. A map is nothing but a group of key-value pairs. Each key-value pairs is called one entry. Now without an existing map object, Is there any chance of existing entry object?

Answer is no because without existing the object of a map, the entry object cannot also exist. That’s why interface entry is defined inside the map interface.  
     Interface Map  {  // Map is an outer interface.
          . . . . . . . . . 
         Interface Entry  { // Entry is an inner interface.
           . . . . . . . . . .  

Now you would have understood from the above real-time example that when have to use an inner class in Java programming. 

Uses of Inner class in Java

There are the following uses of inner class in Java. 
1. The inner class is a valuable feature because it allows you to group classes and interfaces in one place that logically belongs together and to control the visibility of one within the other so that it can be more readable and maintainable.

2. The best use of the inner class is when its functionality is tied to its outer class. That is,  without an existing outer class object, there is no chance of existing inner class object.

Types of Inner class in Java

Based on declaration and behaviors, there are basically four types of inner classes in Java. They are as follows:

1. Normal or Regular inner class: A class created inside another class and outside the method without static modifier is called regular or normal inner class.

2. Method local inner class: A class declared within a method of the outer class is called method local inner class.
Method Local Inner Class in Java3. Anonymous inner class: A class declared without any name at all is called an anonymous inner class.
Anonymous Inner Class in Java4. Static nested inner class: A class declared with static modifier within a class is called a static nested inner class. It is also known as a top-level nested class. It can access only the static members of the outer class.
Static Nested Class in Java Note: The regular, method-local, and anonymous inner class are non-static nested classes.  

Advantages of Java Inner class

There are basically five advantages of inner class in java. They are as follows:
1. Inner class provides a way to group the classes and interfaces in one place that logically belongs together.
2. It can directly access all the variables and methods of the outer class including private.
3. It is used to develop a more readable and maintainable code.
4. It needs less code to write.
5. It increases the encapsulation.

Difference between Nested class and Inner class in Java

Nested class is a class that is defined inside another class. There are two types of nested class: static and non-static. Non-static nested class is the inner class.

Final words 

Hope that this tutorial has covered almost all important points related to inner class in java with realtime examples and programs. I hope that you will have understood the basic idea of types of inner class, uses, advantages.
Thanks for reading!!!
Next ⇒ Member inner class in Java⇐ PrevNext ⇒

Leave a Comment