Inner Classes


Java Inner Class
Java inner class can be called as nested class i.e. declared inside the class or interface.
We use inner classes to group classes and interfaces in one place so that it can be more readable,understandable and maintainable.
It also allows access to all the members of outer class including even private data members and methods.
Syntax of Inner class
[java] class Java_Outer_class{
class Java_Inner_class{
[/java] Advantage of java inner classes
1) Nested classes displays a special type of relationship that is; it can access all the members in a given class (data members and methods) of outer class including even private.
2) Nested classes helps in more readable, understandable and maintainable code as it group classes and interfaces in one place only on the basis of logic provided by the user.
3) Code Optimization: less code to write giving minimum inout and helping to get maximum output.
Difference between nested class and inner class in Java
Inner class is a part of nested class. Non-static nested classes are known as inner classes.

Types of Nested classes
There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.
1. Non-static nested class(inner class)
o a)Member inner class
o b)Annomynous inner class
o c)Local inner class
2. Static nested class

[java] class Outer_Fan{
int a;
private class Inner_Fan{{
public void print(){
System.out.println(“Inner Class”);
void display_Inner(){
Inner_Fan inner = new Inner_Fan();
public class Fan1{
public static void main(String args[])
Outer_Fan outer=new Outer_Fan();

[/java] Output:“Inner Class “

Facebook Comments

Leave a Reply

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