Tutorials Hut

Java Inner Classes

Class within a class is called inner class. Java Inner classes are useful if we need some classes within the same class to do some processing and not required outside class.

The class which is written inside a class is called a nested class and a class that holds a nested class is called outer class.

We can divide nested class into two categories:

  1. Non Static Nested classes.
  2. Static Nested Classes.

Inner Classes / Non Static Nested classes

Non-static classes within a class are non-static nested inner classes.
Based on implementation in code we can categorize them into 3:

  1. Normal Inner Class.
  2. Inner Class In A Method.
  3. Anonymous Inner Class

Normal Inner Class

Class within a class without any static keyword is normal inner class.

In the below example MyOuterClass is outer class and MyNormalInnerClass is inner class and InnerClassExample’s main method is showing how to use them.

Example:

public class InnerClassExample {

public static void main(String[] args) {

MyOuterClass myNormalInnerClass = new MyOuterClass();

myNormalInnerClass.accesInnerClass();
}
}

//Outer class

class MyOuterClass {

public void accesInnerClass() {

MyNormalInnerClass obj = new MyNormalInnerClass();

obj.printFromInnerClass();

}

//Inner class

class MyNormalInnerClass {

void printFromInnerClass() {

System.out.println("Hello from normal inner class.");

}

}

}

Output:

Hello from normal inner class.

Inner Class In A Method

Inner class defined within a class’s method is called inner class in a method.
In the below example MyOuterClass is outer class and MyNormalInnerClass is inner class within method myMethod() of outer class.

Example:

public class InnerClassExample {

public static void main(String[] args) {

MyOuterClass myOuterClass = new MyOuterClass();

myOuterClass.myMethod();

}
}

class MyOuterClass {

//Method

public void myMethod() {

//Inner class

class MyNormalInnerClass {

void printFromInnerClass() {

System.out.println("Hello from method inner class.");

}

}

MyNormalInnerClass myNormalInnerClass = new MyNormalInnerClass();

myNormalInnerClass.printFromInnerClass();

}

}

Output:

Hello from method inner class.

Anonymous Inner Class

A class which is defined along with creation and no other instance of class can be created is called anonymous class and such class within a class is called anonymous inner class.

Syntax:

public class MyOuterClass{
MyAnonymousInnerClass obj = new MyAnonymousInnerClass{
//Code
}
}

In the below example InnerClassExample is acting as an outer class and MyAnonymousInnerClass is an anonymous inner class.

Example:

public class InnerClassExample {

//This can be interface, class or abstract

interface MyAnonymousInnerClass {

void myMethodToPrint();

};
public static void main(String[] args) {

//Anonymous inner class

MyAnonymousInnerClass innerClass = new MyAnonymousInnerClass() {

@Override

public void myMethodToPrint() {

System.out.println("Hello from anonymous class method");

}

};

innerClass.myMethodToPrint();

}

}

Output:

Hello from anonymous class method

Static Nested Inner Class

Static inner class is used similar to normal inner class where a class is defined within another class but with static keyword and has static nature.
Syntax

class MyOuterClass {</p><p>static class MyInnerStaticClass {</p><p>}<br />}

In below example InnerClassExample class is acting as outer class and MyStaticInnerClass is acting as a static inner class.

Example:

public class InnerClassExample {

//Inner class

static class MyStaticInnerClass {

static void printFromInnerClass() {

System.out.println("Hello from static inner class.");

}
}

public static void main(String[] args) {

MyStaticInnerClass myNormalInnerClass = new MyStaticInnerClass();

myNormalInnerClass.printFromInnerClass();

}

}

Output:

Hello from static inner class.

References

Next Article To Read

If you have not read our previous article on Java Exceptions may read as well. You my read our next article on OOPs concetps:


















  • Leave a Reply

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