1)Inner class (also called nested class)
A class that is defined inside another class is called inner class (nested class).
Inner classes are of different types
2)Non static inner class. A class that is defined inside other class and outside any method of that class without the use of static keyword is called non static inner class.
Eg class A {
class B{ }
}
3)When we compile a program that contains an outerclass and innerclass definition, a separate .class file will be generated for the inner class
4)non static inner classes are accessed only using the instance of its enclosing class.
5)A non static inner class can directly access the members (including private members) of its enclosing class
Eg.
class A{
private int i;
class B{
void m1(){
System.out.println(i); // note i belongs to outerclass }
}
}
6)Creating an instance/object of a non static inner class from within the method(s) of its enclosing class (outer class)
class A{
int i;
void m1(){
B b1 = new B();
b1.m2();
}
class B{ void m2(){ System.out.println(i);
}
}
}
7)Creating an instance/object of a non static inner class from outside the outerclass
class A{
int i;
void m1(){ }
class B{ void m2(){ System.out.println(i);
}
}
public static void main(String args[])
{
A a1 = new A();
A.B b1= a1.new B();
b1.m2();
}
}
8) The following modifiers can be applied to declare an inner class
final abstract public private protected static
1) Static inner class (also called static nested class)
A static inner class is an inner class that is defined inside the body of another class using the keyword static
class A {
static class B { void m1(){
}
} }
2) Creating the instance/object of static nested class
A.B b1 = new A.B();
b1.m1();
1)Local class (method local innerclass)
A class that is defined inside a method is called local class or method local inner class
Eg
class A
{
int i;
void m1()
{
class B
{
void m2()
{
System.out.println(i);
} // close local class method
} // close inner class
} // close outer class method
}
class Test1
{
public static void main(String args[])
{
A a1 = new A();
a1.m1();
}
}
2) A method local inner class can only be instantiated from the method in which it is defined ( meaning it cannot be instantiated from out side the method in which defined)
3) A method local inner class cannot be declared public, private, protected,static and transient
4) A method local inner class can be declared abstract or final
1) Anonymous inner class
An inner class that is defined without a name is called anonymous inner class
2)Anonymous inner class can be declared with in a method or as an argument in a method
eg1
package cre;
public class Simplee {
class A
{
public void m1()
{
System.out.println("inside m1");
}
}
class B
{
A a1 = new A() { public void m1() { System.out.println("inside m1 of anonymous"); } };
void m2()
{
a1.m1();
}
}
class Test
{
public static void main(String args[])
{
B b1 = new B();
b1.m2();
}
}
}
The object reference "a1" does not refer to an instance of A, but to the instance of un named (anonymous) sub class of A.
3) Defining an anonmous inner class inside the argument of a method
Ex
interface A { void m1(); }
class B { void m2(A a){ } }
class C
{
void m3()
{
B b1 = new B();
b1.m2(new A(){ void m1(){ System.out.println("method m1"); } } );
}
}
class Test
{
public static void main(String args[])
{
C c1 = new C();
c1.m3();
}
}
No comments:
Post a Comment