Java Inner class is also known as the nested class is a class within the class, method, or block declared inside class or interface. The use of the inner class is to group the classes or interfaces logically in one place so that it becomes easy to read and maintain.
Java Inner class is of two types:
A. Non-static nested classes
- Member inner class
- Anonymous inner class
- Local inner class
B. Static nested classes
A. Non-static nested classes in Java: (Inner class)
It is one of the types of java Inner class. As we have learned from the Access modifiers that we cannot declare a class with ‘private’ modifiers. But the inner class, that is the member of the other class is declared then we can have the inner classes as ‘private’.
An inner class is of the following types:
- Member inner class
- Anonymous inner class
- Local inner class
1. Member inner class:
It is a Non-static nested class (inner class) that is declared inside a class and outside all the methods or blocks. If we declare it as private then that inner class cannot be invoked from an object outside the outer class of that inner class.
Syntax for member inner class in Java:
1 2 3 4 5 6 7 8 9 10 11 | class OuterClass { //code class InnerClass { //code } //methods } |
Let us use it in a program:
In the following example, we declare the private inner class within the outer class, which also contains a method ‘displayingInner()’ that instantiates an inner class and invokes the method that is present inside the inner class.
And the Main class instantiates the Outerclass class and invokes the method ‘displayingInner()’, that is the nesting done here.
Note: That the InneerClass cannot be accessed outside the outer class because of its private modifier but it can be accessed by the method inside the OuterClass.
To demonstrate the example if Member inner Class in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class OuterClass { // inner class declaring as private private class InnerClass { public void displayInner() { System.out.println("Inner class Acceseed"); } } // method for accessing the inner class void displayingInner() { InnerClass in = new InnerClass(); in.displayInner(); } } public class Main { public static void main(String args[]) { // Instantiating the outer class OuterClass out = new OuterClass(); // Accessing the displayingInner method. out.displayingInner(); } } |
Output:
Inner class Acceseed
2. Anonymous inner class:
Anonymous inner class is a class that does not contain the class name. They are used in a class or interface whenever the user needs to override the methods. The declaration and instantiation of the Anonymous inner class are done at the same time.
There are two ways in which this class can be created. They are:
- First Subclass or maybe abstract
- Second interface
Let us see it individually:
Example of Anonymous inner class Using class in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | abstract class Dog { abstract void bark(); } public class TestAnonymousInner { public static void main(String args[]) { //An anonymous class Dog d = new Dog() { void bark() { System.out.println("The dog is barking"); } }; d.bark(); } } |
Output:
The dog is barking
Example of Anonymous inner class using interface in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | interface Dog { void display(); } public class Flavor2Demo { public static void main(String[] args) { // An anonymous class Dog d = new Dog() { public void display() { System.out.println("I am a good Dog"); } }; d.display(); } } |
Output:
I am a good Dog
3. Local inner class:
The class that is declared within the method of an outer class is called the Local inner class or method-local inner class. As the name suggests local, that is its restriction is within that method only, just like a local variable.
Example to demonstrate the local inner class in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class OuterClass { void MethodOuter() { System.out.println("Outer Class Method"); class LocalClass //Local Inner class { void MethodInner() { System.out.println("Inner Class Method"); } } LocalClass c = new LocalClass(); c.MethodInner(); } public static void main(String args[]) { OuterClass o = new OuterClass(); o.MethodOuter(); } } |
Output:
Outer Class Method
Inner Class Method
B. Static nested classes:
If a class within another class that is the nested class has the static modifier declared in it, then it is called a static nested class. As this class is applied static, it will not be able to access the instance variables and methods of the outer class, it can only access the static member of the outer class.
Example: Accessing static method of a static class in Java,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class OuterClass { void MethodOuter() { System.out.println("Outer Class Method"); class LocalClass //Local Inner class { void MethodInner() { System.out.println("Inner Class Method"); } } LocalClass c = new LocalClass(); c.MethodInner(); } public static void main(String args[]) { OuterClass o = new OuterClass(); o.MethodOuter(); } } |
Output Static nested classes:
I am Inner Class