Inner classes are the classes which are defined inside a class or interface.
Basic Syntax for Inner Class in Java is
class OutrClass
{
class InnrClass
{
Text goes here…
}
}
If the inner class is compiled using the command javac OutrClass.java. We will get two class files instead of one. OutrClass.class and OutrClass$InnrClass.class are the two class files formed after compiling of a file with inner class.
- SEE: Java interview Q&A available here
How to Access Inner Class?
Despite the fact that a separate class file is created for nonstatic inner class; it cannot be accessed through compiling the outer class. The access to inner class is provided only through the instance of outer class. Inner class can be executed by running the class file generated for it which in turn will run the main() method generated for the inner class. The class file for inner class according to the syntax is OutrClass$InnrClass.class. The thumb rule for accessing an inner class is an instance of outer class is required to access the instance of inner class.
Types of Inner Class
The Inner classes can be classified into four types.
- Static Nested Inner Classes.
- Member Inner Class.
- Method-Local Inner Classes.
- Anonymous Inner Classes.
Static Nested Inner Classes:
Static nested inner classes can access the instance variable of outer class. The instance variables of static nested inner class also contains access modifies like default, private, public and protected. In Java, interface also contains instance variables and access modifiers.
Example Program for Static Nested Inner Class
class Outr1
{
static class Innr1
{
void go1()
{
System.out.println(” An inner class ref is: ” +this);
}
}
}
public class 1Test
{
public static void main(String[] args)
{
Outr.Innr n1=new Outr.Innr();
n1.go1();
}
}
The output is “Inner class reference is: Outr$Innr@19e7ce87”.
Member Inner Class
Member inner classes are specific to a certain instance. Member inner classes have access to all the methods and fields. This type of inner classes are created within a class but declared outside a method.
Example Program for Member Inner Class
public class Outr
{
private int x1= 1000;
public void makeInnr()
{
Innr in1=new Innr();
in.seeOutr();
}
class Innr
{
public void seeOutr()
{
System.out.println(“Outr X1 is ” + x1);
System.out.println(“An Inner class ref is ” + this);
System.out.println(“An outer class ref is ” + Outr.this);
}
}
public static void main(String [] args)
{
Outr o1= new Outr();
Innr i1= o.new Innr();
i1.seeOutr();
}
}
Output:
Outer x1 is 1000
Inner class ref is Outr$Inner@4dfd9726
Outer class ref is Outr@43ce67ca
Method Local Inner Class
Method Local Inner Classes are defined inside a method by enclosing a class. Method inner class can be used only if the inner class is instantiated in the same method. Only abstract and final modifiers are allowed in method local inner class. Only final local variables are allowed in this class. The reason behind declaring the local variables as final is to hold the variable in stack till the method is on stack. If the variable is not declared as final, it will be stored in heap. Method local inner class can be declared as abstract and final but declaring it as private, protected and static is not allowed. Declaring the class as abstract and final at the same time is also not allowed.
- Download: Final year projects for free
Example Program for Method Inner Class
class Outr
{
void outrMethod()
{
System.out.println(“An innner and outerMethod”);
// Inner class is local to outrMethod()
class Innr
{
void innrMethod()
{
System.out.println(” An inside innrMethod”);
}
}
Innr y1=new Innr();
y1.innrMethod();
}
}
class MethdDemo1
{
public static void main(String[] args)
{
Outr x1= new Outr();
x1.outrMethod();
}
}
Output
Inside outrMethod
Inside innrMethod
Anonymous Inner Class
Anonymous inner classes do not have any name and they are declared as “subclass” for a particular type. These classes can be instantiated only once and usually declared inside as method with parenthesis and semicolon. Anonymous inner classes are accessible at a point where it is defined. Since no name is used to declare these classes; constructors are also not allowed. Anonymous inner classes cannot be declared as static. These classes are commonly used in GUI applications while adding an action listener in a widget.
Example Program for Anonymous Inner Class
class FlavrTwoDemo
{
// An anonymous class that implements Hello interface
static Helo h1= new Helo()
{
public void display()
{
System.out.println(“You are in an anonymous class”);
}
};
public static void main(String[] args)
{
h1.display();
}
}
interface Helo
{
void display();
}
Output
You are in an anonymous class