Nested classes in Java

Java nested class and java inner class is the one of most confusing and important topics. Firstly, I want to tell Java nested class and java inner class is not the same concept, most of the programmers misunderstood it. In java, inner classes are part of nested classes. Most of the programmer knows about nested class in java, but the problem starts when the interviewer asks what is the use of nested classes or the use of inner class in java and java inner class example? In this post we will discuss the types of inner classes in java like java anonymous class, java static inner class, local inner class in java.

Here is the table content of the article will we will cover this topic.
1. What is the nested class in Java?
2. Important points about the nested class?
3. Types of nested classes?
4. Static nested classes
5. Non-static nested class(Inner classes)

6. Advantages of nested class in Java

What is the nested class in Java?

In java, we can define a class inside another class, that class is known as java nested class. The class in which contains the other class is known as the outer class and the contained class is known as inner class. The concept of nested class was introduced to achieve logically grouping of classes and provide more encapsulation. We will learn it in a separate topic i.e. Why we need the nested class?

    // Body of OuterClass
        /// Body of InnerClass

The OuterClass is containing the InnerClass, this is known as a nested class in java. The nested classes work together and provide better readability of the program.

The nested classes are divided into two categories, ie. Static nested class and non-static nested class(inner classes). The non-static nested classes are considered as inner classes. We will discuss it in detail.  

class OuterClass 
     // Body of OuterClass
    static class StaticNestedClass 
        // Body of StaticNestedClass 
    class InnerClass 
        // Body of OuterClass

As we know a class can be used as a member of its enclosing class. A nested class can be static and non-static. The non-static nested classes are known as inner classes and static nested class is considered a nested class in java.

Important points about nested class

Nested class scope

The scope of the nested class binds with its enclosing class. The InnerClass doesn’t exist without the OuterClass and it totally depends on OuterClass. You can’t access the InnerClass without using the OuterClass.

Access class member

The InnerClass can access the members of the OuterClass Wheather they are private or not. A static nested class can access only the static member of OuterClass. But the OuterClass can’t access any member of InnerClass.

Access modifier

We can use any access modifier with a nested class. As we know we can’t use a private or protected access modifier with a normal class. But nested allow use to InnerClass class can be declared privatepublicprotected, or default.

Types of nested classes

There are two types of nested classes.
1. Static nested classes.
2. Non-static nested class(Inner classes)
i). Nested inner class or member inner class
ii). Local inner class or method local inner class
iii). Anonymous inner class

Nested classes in Java

static nested class

1. Static nested class: As we know we can declare a class within another class, such classes are known as nested classes. So that we can declare a nested class with the static keyword, and it is called a static nested class.

Non-static nested class(Inner class in java)

2. Nested inner class or member inner class: It is the simplest form of inner classes. We can declare a non-static class within another class is called a nested inner class. It is also called member inner class because it behaves like a member of the outer class. You can’t define it inside the method.

3. Local inner class or method local inner class: A class that is defined inside a block or method is called a local inner class. Generally, programmers declare the class in a method that’s why it is called method local inner class. You can declare a class inside of for loop, or an if clause.

4. Anonymous inner class: An inner class that can be declared without the name is known as an anonymous inner class. You must override the method of class or interface. There are two ways:
1. By use of Class
2. By use of Interface

Advantages of nested class in Java

1. By use of nested classes, the inner class can access all variables and methods of outer
class. It can access private variables or methods.
2. It provides good maintains and readability of code.
3. It requires less code that lead to code optimization.
4. The concept of nested classes to increase the use of encapsulation.

Leave a Comment