Week 127 — What are nested classes and how do they work?
Question of the Week #127
What are nested classes and how do they work?
6 Replies
Java allows declaring classes within other classes.
In that example, two nested classes are declared within
OuterClass
. While StaticNestedClass
is marked with static
, this is not the case with NormalNestedClass
.Nested classes can be accessed using the name (either the fully-qualified name or the simple name if it is imported or in the same package) of the outer class, a
.
and the name of the nested class. The constructor of static
nested classes can be called in the same way but an object of the outer class is required for creating instances of non-static
nested classes. For this, the object is written before the new
operator.
As the creation of instances of non-static
nested classes requires an object of the outer class, that object is available within non-static methods (and constructor) of that (non-static
) nested class.
The outer class can access private
members of the nested classes and the nested class can also access private
members within the enclosing class.📖 Sample answer from dan1st
if you have a super class which gets implemented by other classes and if those other classes are static and inside of the super class then they're called nested classes
Submission from creepertv_1
In Java nested class means a class written inside an another class. Just like we can have functions or you can say method inside a class, we can also write a class inside another class that is what we call as nested class.
It is used to group classes that are only used inside one class
There are two types of nested classes
1. Static nested class 2. Non Static nested class
Static Nested Class: We can create this with the use of "static" keyword.
It doesn't need object of the outer class to be created. It means if you want use Static Nested Class in any another class then you can directly create object of that class without creating object of it's parent class
Non Static Nested Class: It is normal nested class. If you want to create of this class then firstly you have to create object of it's parent class. It can access all variables and methods of it's parent class.
Now we'll understand how the nested classes work behind the scenes:
Firstly the compiler will create a separate .class file for every class you write in the program
When a non static inner class is created, it holds a reference to the outer class object. This means the inner class constructor takes the outer class object as a hidden argument. This allows it to access all variables and methods of the outer class.
And if talk about static inner class JVM treats it like a regular class just grouped inside another class.
Based on memory
A static nested class behaves like a normal class, so it’s more memory efficient because it doesn’t need to access outer class stuff.
An inner class carries a reference to the outer class, so it uses more memory. But overall, unless you're creating millions of objects, this doesn't cause performance issues. The benefit of clean code usually outweighs the memory cost.
⭐ Submission from kherasam18
Nested classes are classes that are defined within another class. It's mostly used in arranging classes that are only relevant to one class (or their "outer class").
There are two types of nested class:
i) Static nested class
ii) Inner class
Nested classes are scoped inside their outer class, and can have access modifiers (
private
, public
, protected
, ...) like any other normal classes.
Two other important details about nested classes:
- Static nested class cannot access outer classes fields (can access static fields), due to it's static property. This type of class can be initialised without initialising the outer class.
- Non-static (Inner) nested class can access outer classes fields, this type of class cannot be initialised without initialising the outer class.
Inner Nested Class Example:
Static Nested Class:
⭐ Submission from daysling