Inner classes in Java
A class that is defined within another class (or interface) is known as nested class. In other words, nested class is a class contained within another class or interface.
Java provides four types of nested classes-
A class defined within a method, constructor, or initialization block is called local class, whereas a non-local class defined inside a class.
Static Nested Class
- A nested class declared with static keyword is called static nested class.
- A static nested class is also known as static inner class.
- You can apply private, protected, default or public access modifier to a static nested class.
- An static inner class can have constructors, static and non static members.
- A static nested class is allowed to access static members of its outer class.
- On compiling the code having static nested class, the compiler produces two .class files- Outer.class and Outer$StaticNested.class.
-
There are two ways of creating objects of static nested class-
- Within outer class- Inner referenceVariable = new Inner();
- Outside outer class- Outer.Inner referenceVariable = new Outer.Inner();
Syntax
public class Outer
{
access-modifier static class Inner
{
//members of static inner class
}
}
Example of static nested class
class Outer
{
static class Inner
{
public void display()
{
System.out.println("I am a inner class.");
}
}
public void show()
{
Inner i = new Inner();
i.display();
}
}
public class Test
{
public static void main(String args[])
{
Outer o = new Outer();
o.show();
Outer.Inner i = new Outer.Inner();
i.display();
}
}
Output
I am a inner class.
I am a inner class.
Inner Class
- You can apply private, protected, default or public access modifier to a inner class.
- It is also known as member class.
- An inner class can have constructors, variables, and methods.
- It cannot define a static method and nonfinal static variables. Only final static variables can be defined in it.
- On compiling the code having inner class, the compiler produces two .class files- Outer.class and Outer$Inner.class.
-
There are two ways of creating objects of inner class-
- Within outer class- Inner referenceVariable = new Inner();
- Outside outer class- Outer.Inner referenceVariable = new Outer().new Inner();
Syntax
public class Outer
{
access-modifier class Inner
{
//members of inner class
}
}
Example of inner class
class Outer
{
class Inner
{
public void display()
{
System.out.println("I am a inner class.");
}
}
public void show()
{
Inner i = new Inner();
i.display();
}
}
public class Test
{
public static void main(String args[])
{
Outer o = new Outer();
o.show();
Outer.Inner i = new Outer().new Inner();
i.display();
}
}
Output
I am a inner class.
I am a inner class.
Local Inner Class
- The local inner class is defined within static or instance method of a class.
- You cannot apply private, protected, default or public access modifier to a local inner class.
- It class can have constructors, variables, and methods.
- You can only create an object of local inner class inside a method, and object creation must appear after the declaration of the local inner class.
- It can access all the variables and methods of its outer class. It can only access final local variables of the method in which it is defined.
Syntax
public class Outer
{
access-modifier return-type methodName(parameters)
{
class Inner
{
//members of inner class
}
}
}
Example of local inner class
class Outer
{
public void show()
{
class Inner
{
public void display()
{
System.out.println("I am a inner class.");
}
}
Inner i = new Inner();
i.display();
}
}
public class Test
{
public static void main(String args[])
{
Outer o = new Outer();
o.show();
}
}
Output
I am a inner class.
Anonymous Inner Class
-
A class that has no name is called anonymous inner class. It is created in two ways-
- Combine object creation with inheritance.
- Combine object creation with implementing an interface.
- It can override zero or more methods of the inherited class, but it must implement all the methods of the interface.
-
An instance of the anonymous class can be used in three ways-
- You can pass it as a method parameter.
- You can return it from a method.
- You can assign it to any variable(instance variable, static variable, or local variable).
- If you define an anonymous inner class in a method, then it can only access final variables of that method.
- An anonymous inner class is mostly used in Android programming.
Syntax
new ClassName(){
//Override method
}
Example of Anonymous Inner Class
abstract class Shape
{
abstract public void show();
}
public class Test
{
public static void main(String args[])
{
Shape s = new Shape(){
public void show(){
System.out.println("I am an anonymous inner class.");
}
};
s.show();
}
}
Output
I am an anonymouse inner class.