Surge of Covid cases in London health workers sparks fear of spread on wards

Covid infection rates among doctors, nurses, and other hospital and care home staff have risen more than fivefold over the past month in London, scientists have discovered. The figures — provided by…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Inner class in Java

Sometimes we can declare a class inside another class, such type of class are called inner class.
Inner class concept includes in Java version 1.1 to fix GUI bugs as the part in Event Handling but because of the powerful feature of inner class slowly developer started using in normal programming practice.

Ex 1:- University consist may department, without existing university there is no chance of existing department, hence we can declare department class inside university class.
Ex 2:- Map is a group of key-value pair and each key-value pair is an entry without existing map object, there is no chance of existing entry object, hence interface Entry define inside map interface.

If we declare any named class directly inside a class without a static modifier, such type of inner class is called normal or regular inner class.

Inside the inner class, we can’t declare any static member, hence we can’t declare the main method. so that we can’t run the inner class directly from CMD.

CASE STUDY

2. Within the inner class, this always returns the current inner class object. if we want to refer current outer class object then we have to use the outer class name.

3. Within the inner class, we can declare another inner class. nested inner class is possible.

2. Method Local Inner Class

Sometimes we can declare a class inside a method such type of inner class is called Method local inner class. the main purpose of method local inner class is to define a method specific functionality.
we can access method local inner class classes only within the method .

Important points:

4. From method local inner class, we can’t access the local variable of the method in which are declare inner class, if the local variable declares as final then we can access.

3. Anonymous Inner Class

Sometimes we can declare inner class without name such type of inner class is called anonymous inner class.

Based on deceleration and behavior, there are three types of the anonymous inner class.
1. Anonymous inner class that extends a class

2. Anonymous inner class that implements an interface.
3. Anonymous inner class that defines inside arguments.

Normal Java Class v/s Anonymous inner class

4. Static Nested Class

Sometimes we can declare an inner class with a static modifier, such type of inner class are called static nested class.
In case of normal inner class without the existing outer class object, it is not possible to create an inner class object but in case of static nested class without the existing outer class object, there may be a chance of existing nested class object.

Note:- Nested class object are not strongly associated with the outer class object.

Difference between Normal inner class and Static nested class

Thanks for reading this. Please clap if u like this post.

Add a comment

Related posts:

My 2020 WFH Setup

This has been a crazy year. This is the year I realized that I love being out sometimes, even if I am an introvert! I switched my job and started my new position just a week before India went into…

I Gave Myself Permission to Get Fat

The first time I became fat was during the time we were adopting our daughter. I was 29 years old. A neighbor was pregnant and we both ate our way through our baby waiting. She lost most of her extra…

How stern do I have to be?

How stern do I have to be?. To quit wasting time on my phone? Stern like the Buddha who lit himself. Stern like a marathon runner with a broken leg..