Argument-Defined Anonymous Inner Class:
This type inner class can be instantiated using new keyword. It is declared, defined and automatically instantiated as part of a method invocation.
class A{
void do(){
B b=new B();
b.call(new C(){
public void show(){
System.out.println("This is argument-defined anonymous inner class");
}
});//This end quite different than other anonymous inner class.
}
}
interface C{
public void show();
}
class B{
void call(){
......
}
}
This type of inner class is also called as Method Local Inner class.
Its important to remember that, this class is declared only during method invocation and hence it is accessible only after the method invocation.
Can a class be declared static??
The inner class can be marked static and hence it is called Static Nested class.
It is a top level nested class. That means that an instance of the outer class is not required to instantiate this class.
class A{
static class B{
void show(){
System.out.println("This is a static nested class");
}
}
}
The class B can be instantiated by A.B b=new A.B(); as simple as that.
As this is static class it will not have access to any non static member of the class
Sunday, May 4, 2008
Method Local inner class and static inner class.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment