What is inner class?
- Java inner class or nested class is a class which is declared inside the class or interface.
- Inner class is a part of nested class.Non-static nested classes are known as inner classes.
- There are basically four types of inner classes in java.
- Nested Inner class
- Method Local inner classes
- Anonymous inner classes
- Static nested classes
Nested Inner class
- Nested Inner class can access any private instance variable of outer class.
- we can’t have static method in a nested inner class because an inner class is implicitly associated with an object of its outer class so it cannot define any static method for itself.
Example:
class Outer {
// Simple nested inner class
class Inner {
public void show() {
System.out.println("im a inner class method");
}
}
}
public class Innerclass {
public static void main(String[] args) {
Outer.Inner in = new Outer().new Inner();
in.show();
}
}
Output:
im a inner class method
Method Local inner classes
- Inner class can be declared within a method of an outer class.
- In the following example, Inner is an inner class in outerMethod().
Example:
class Outer {
public int var1=10;
void outerclass_method()
{
System.out.println("im method of outer class");
class Inner {
public int var2=20;
public void show() {
System.out.println("im a inner class method and im declaring it in inside the outer class method");
System.out.println("im accessing private variabl of the outside of the class="+var1);
}
}
Inner y = new Inner();
y.show();
}
}
public class Innerclass {
public static void main(String[] args) {
Outer in = new Outer();
in.outerclass_method();
}
}
Output:
im method of outer class
im a inner class method and im declaring it in inside the outer class method
im accessing private variabl of the outside of the class=10
Static nested classes
- Static nested classes are not technically an inner class.
- They are like a static member of outer class.
Example:
class Outer {
private static void outerMethod() {
System.out.println("inside outerMethod");
}
// A static inner class
static class Innerclass {
public static void main(String[] args) {
System.out.println("inside inner class Method");
outerMethod();
}
}
}
Output:
inside inner class Method
inside outerMethod
Anonymous inner classes
- Anonymous inner classes are declared without any name at all. They are created in two ways.
- As subclass of specified type
- As implementer of the specified interface
- NOTE: Any anonymous inner class can implement only one interface at one time. It can either extend a class or implement interface at a time.
Example:(As subclass of specified type)
package innerclass;
class Demo {
void show() {
System.out.println("i am in show method of super class");
}
}
class Innerclass {
// An anonymous class with Demo as base class
static Demo d = new Demo() {
void show() {
super.show();
System.out.println("i am in inner class of anonymous");
}
};
public static void main(String[] args) {
d.show();
}
}
Output:
i am in show method of super class
i am in inner class of anonymous
Example:(As implementer of the specified interface)
interface Demo {
void show();}
class Innerclass {
// An anonymous class with Demo as base interface
static Demo d = new Demo() {
public void show() {
System.out.println("i am in annonimas class");
}
};
public static void main(String[] args) {
d.show();
}
}
Output:
i am in annonimas class
Advantages of inner class
- Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
- Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
- Code Optimization: It requires less code to write.