I conform to Pat.Believe it or not, there are advantages to Java's inner classes. But before we go into that, I'll provide a short background on inner classes.
Inner classes nest within other classes. A normal class is a direct member of a package, a top-level class. Inner classes, which became available with Java 1.1, come in four flavors:
Static member classes
Member classes
Local classes
Anonymous classes
An Inner class is a nested one, either it may be defined inside a class or inside a method, sometimes, can be anonymously.
Inner classes allow us to achieve more encapsulation and thus more security.
1.Member Class
----------------
Here, Inner class is termed as Member class.
class Outer
{
class Inner
{
}
}
*Creating the object of Inner class needs an object of outer class.
new Outer().new Inner();
To hold the object of the Inner class, the reference must be of the form,
Outer.Inner obj;
Finally,
Outer.Inner obj=new Outer().new Inner();
We can have either way,
Outer out=new Outer();
Outer.Inner obj=out.new Inner();
*An Inner class can have access to Outer class members, but the reverse is not true.
*An Inner class is just like a member of a class, and allowed all the modifiers like public,private,protected,default,static,final,abstract etc.
2.Static Inner Class
=====================
* Creation of an object of the Static Inner class doesn’t require an Outer class object, just requires the name of the Outer class.
new Outer.StaticInner();
To hold the object of the Inner class, the reference must be of the form,
Outer.StaticInner obj;
Finally,
Outer.StaticInner obj=new Outer.StaticInner();
No other way.
3.Local Inner Class
=====================
A class defined inside the body of a method.
It’s like local member of a method, can’t have access specifiers like public,private,protected.
Can have only final,abstract.
We can directly execute the Local Inner class by calling the enclosing function.
If we want any method written inside the Local Inner class, we’ll call that method, using an object created to that Local class, in the enclosing function.
We can’t have static data inside the Local Inner class.
We can’t access a non-final variable (of the enclosing function) inside a Local Inner class method.
Example:
--------
//A Static Inner class Example
class Outer
{
void enclosingFun()
{
System.out.println("Hello Outer fun");
class Local
{
void localFun()
{
System.out.println("I'm Local");
}
}
Local l=new Local();
l.localFun();
}
static class StaticInner
{
void display()
{
System.out.println("Hello Static Inner fun");
}
}
}
public class LocalInnerDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer o=new Outer3();
o.enclosingFun();
}
}
4. Anonymous class
==================
A class defined inside the body of a method; and is of no name.
Example
========
//A Static Inner class Example
class Outer4
{
void display()
{
System.out.println("Displayed in Outer class");
}
}
public class AnonymousDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer4 o=new Outer4(){
void display()
{
System.out.println("Displayed in Anonymous class");
super.display();
}
};
o.display();
}
}