Nested Class in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

In Java, just like nested Loops, we have Nested Classes, i.e., we can define a class inside another class.

We can group classes with similar properties under an outer class, so they are together. It increases the encapsulation and readability of the code. The nested class comes under the principles of Object-Oriented Programming (OOP).

We can not define normal classes as private. But Java treats the nested class as members of the outer class, and we can define them as private.

Properties of Nested classes in Java

  • In Java, a nested class is a class that is defined inside some other class.
  • Nested classes are used to group certain classes to improve the readability of the code.
  • The scope of a nested class is the same as its outer class.
  • Nested classes can access any member of the outer class, even if the outer class is private.

Note: Outer class does not have access to the Inner class if the inner class is private. We use nested classes to group into different classes and to increase security by limiting the scope of classes.

Types of Nested classes in Java

A nested class can either be defined as a static type or a non-static type. Types in detail:-

  • Static Nested Classes
  • Non-static Nested Classes (or Inner Classes) We can further divide the Non-static Classes into -
    • Member Inner Classes
    • Local Inner Classes
    • Anonymous Inner Classes

Classification of Nested Classes in Java

Static Nested Classes

A static class is a class that is created inside a class. A static variable is common to all the instances of that particular class. Similarly, a static class can access all the instance functions of the outer class.

To do this, we will have to make objects of child classes and refer to them. We can access all the static functions of the outer class without object creation.

Syntax:

We can create an object of this class like this:

Let us see an example of using the static nested class and accessing its method from other functions.

Output: Code Output Nested Class Java

Explanation: Here, we have created a nested class. The parent_class is the outer class, and the static_child_class is the inner class. The main() function of the parent_class access the inner class methods with the object.

Non-static Nested Classes (Inner Classes)

A non-static nested class or inner class is a class within a class. We do not define it as static so that it can directly use all the functions and variables of the outer class.

From the inner class, if we want to access any static method of the outer class, we do not need any object; we can call it directly.

1. Member Inner Classes

Syntax:

To access the non-static inner class from the static main method or any other static method, we have to create objects of the parent_class as well as the child_class. The syntax for these objects:

Let us see an example of using the non-Static nested class and accessing its method from other functions:-

Output: Code Output Nested Class Java

Explanation: Here, we are creating an object of the parent class and, with the help of it, creating the object of the child class. We are accessing the methods of the child_class using the childObj. We had to do this because the child_class is non-static, while the main method is static.

2. Local Inner Classes

A Local Inner class is a class that is defined inside any block, i.e., for block, if block, methods, etc. Similar to local variables, the scope of the Local Inner Class is restricted to the block where it is defined.

Syntax:

Let us see an example of using the Local inner class and accessing its method:-

Output: Code Output Nested Class Java

Explanation: Here, we are defining a class inside an if block. And we are creating an object of child_class and calling it from the same block. The child_class is only accessible from the if block and can not be called from outside.

3. Anonymous Inner Classes

Anonymous Inner class is an inner class but without a name. It has only a single object. It is used to override a method. It is only accessible in the block where it is defined.

We can use an abstract class to define the anonymous inner class. It has access to all the members of the parent class. It is useful to shorten over code. Basically, it merges the step of creating an object and defining the class.

Syntax:

Let us see an example of Anonymous inner class and accessing its methods:-

Output: Code Output Nested Class Java

Explanation: The Printer object is used to define the anonymous inner class. Then we are calling the print() method of the anonymous inner class using the object we just defined.

Conclusion

  • Nested classes in Java are classes inside another class.
  • We use Nested Classes to group into different classes and to increase the security by limiting the scope of classes.
  • While compiling a Nested class, multiple .class files are generated, 1 for each class.
  • In Nested classes, the outer class does not have access to the data and methods of the inner class if the inner class is private.
  • We have two types of Nested Classes
    • Static Nested Class
    • Non-Static Nested Class