Last Updated: 20 November, 2023
A class that is defined inside another class as a member is called a nested class in Java. Nested classes create more encapsulation and provide more readable and maintainable code because they logically group classes in one place.
Java Nested classes are divided into two categories:
A non-static nested class is a class that is defined inside another class without the static keyword. A non-static nested class is also known as an inner class in Java.
Since the inner class is defined inside the outer class, we must first instantiate the outer class before we can instantiate the inner class.
Syntax:
Non-static nested classes are further divided into three different types based on their implementation in Java, as given below.
Let's see each non-static nested class (Inner class).
A class that is defined inside another class without a static keyword and outside a method is known as a "member inner class" in Java.
Syntax:
To access inner class properties first, we need to create an instance of an outer class because the inner class is defined as a member of the outer class. Using the outer class instance, we create the instance of the inner class.
A class that doesn't have a name is known as an anonymous class in Java. An anonymous class is always defined within another class. Hence, it is also known as an anonymous inner class.
An anonymous inner class can be useful when making an instance of an object with certain “extras”, such as overriding methods of a class or interface, without having to actually subclass a class.
📝 The anonymous inner class name is decided by the compiler.
In Java, we can create an anonymous inner class in two ways:
Syntax:
A Java class that is defined inside a block or method is known as a "local inner class" or "method local in class" in Java. The scope of the local inner class is inside the block or method only; it can’t be accessed from outside the block or method in which it is defined.
A local inner class cannot be public, protected, private, or static, but it can be final, abstract, or strictfp in Java.
Syntax:
The local inner class can access outer class properties such as fields and methods.
A method local inner class can extend an abstract class or implement an interface.
A class that is defined inside another class with a static keyword is known as a static nested class in Java. A static nested class cannot access outer class non-static properties but can access static properties. A static nested class is not associated with any particular instance of the outer class. This means that a static nested class can be accessed and instantiated without having to create an instance of the outer class.
Syntax:
A static nested class can have static and non-static fields and methods.
A static nested class cannot access non-static members of its enclosing class.
A static nested class can access static members of its enclosing class, including private members.
An outer class will never be a static class in Java.
To access a static nested class, we can use the following syntax:
OuterClass.StaticNestedClass instance = new OuterClass.StaticNestedClass();
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why do we need nested classes in Java?
Ans. The nested classes provide more readable and maintainable code because they logically group classes in one place. Let's consider the below scenarios as given for more clarity:
Scenario 1: If an individual class 'B' is only used by a single class 'A' so it's a good approach to declare class 'B' as a nested class in class 'A' so that no other class can access 'B' Class.
Scenario 2: If all the class objects are part of the outer object, then it is easier to nest that class inside the outer class. That way, all the members of the outer class can access all the objects of the inner class.
What is the difference between a nested class and an inner class in Java?
A class that is defined inside another class is called a nested class. Nested classes can be static or non-static. Non-static nested classes are known as inner classes in Java.
The inner class requires an instance of the outer class for initialization, and they are always associated with an instance of the enclosing class.
What are the benefits of the nested class in Java?
Ans. Nested classes provide the following benefits in Java:
What are potential drawbacks of using nested classes?
Ans. In Java, there are the following potential drawbacks of using nested classes:
Increased complexity in code due to multiple levels of class nesting.
Potential for memory overhead if too many instances of nested classes are created.
Code maintenance might become challenging if the relationships between nested classes become convoluted.
Can we define an inner class within an interface?
Yes, Java allows us to define an inner class within an interface. The inner class is implicitly public and static inside the interface.
What is the difference between an inner class and a subclass in Java?
Ans. There are big differences between inner classes and subclasses:
What is the use of local classes in Java?
Ans. When we require a class that is only relevant within a certain method or block, local classes come in handy since they allow us to create the class as needed. They have access to the members of the class that encapsulates them, but they do not have access to anything outside of the scope in which they are declared.
Can a local class access local variables of the enclosing method?
Ans. Yes, a local class can access local variables of the enclosing method, but those local variables must be declared as final or effectively final (variables that are not reassigned after their initial assignment).
When might we use an anonymous class in Java?
Ans. Anonymous classes are often used for creating small, one-time-use instances of classes that implement interfaces or extend classes. They are concise for situations where we don't want to create a separate named class.