inner classes in java

in this post we will discuss about the inner classes in java. so what is inner class and when we should go for inner classes.

Sometimes when we declare a class inside another class, such type of class is called inner class.

in above example we declared two classes car class and engine class, without existence of car object there will be no existence of Engine class hence we declared Engine class inside Car class.

There are four types of inner classes in java:-

1. Normal or regular inner class
2. Method local inner class
3. Anonymous inner class
4. Static nested class

1. Normal or regular inner class:- A class declared inside another class is called normal or regular inner class.

2. Method local inner class:- when we declare a class inside a method such type of classes are called method local inner class. the main purpose of method local inner classes is to define method specific functionalities. the scope of method local inner classes is the scope of the method where it is declared.

3. Anonymous inner class:- sometimes when we declare a inner class without name such type of inner classes are called anonymous inner class

Anonymous inner class can be categorized in three categories

i. Anonymous inner class that extends a class.
ii. Anonymous inner class that implements an interface.
iii. Anonymous inner class that are defined inside a method argument.

4. Static nested class:- when we declare inner class with static modifier such type of inner class are called static nested class.

is it possible to declare main method inside inner class?

it is not possible to declare main method inside inner class but in static nested class we can declare.

inner classes in java

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.