Nested Classenotess
Nested Classenotess
class Outer
{
class Inner
{}
}
ex:
class Outer
{
int a=10, b=201;
void ml()
{
m2(); //not possible
}
class Inner
{ int x=100,y=200;
void m2()
{
m1();
}
}
}
Note:-
1. Outer.class
Outer$Inner.class
1.
class A
{ class B
{}
}
2.
class Testa
{
Void m1(){}
void m2(){cLass X{}}
Void m10(){}
}
Ex:-
class BÄnk
{ class Account
{
}
}
Ex:-
class Cotg
{ class Dept
{
}
}
Note:- (1)The Account Functionally can exit with the presence of Bank.
Because to create the object of Account class , Bank class object in mandatory.
(2) To create the object of inner class, outer class object in mandatory.
Note:-
A class which is declare inside a method is called method local inner class
In application level there are so many method. It’s possible each method contain inner
classes
Inside a method we can declear multiple classes
Its possible one class extend another class
The scope of the class is only within the method
Outer class five modifires are applicable : public, default, final, abstract, strictfp
Inner class eight modifires are applicable : public, default, final, abstract, strictfp ,
static, private, protected
Inner class abstract (cannot be instantiated)& final (inheritance not possible) modifier
are possible
Ex:- 1
Note:-
The normal inner class can access both static and instant
member of outer class.
The static inner class can access only static member of outer
class.
In normal inner class static declarations are not possible
In static inner class static declarations are possible
In normal inner class main method, static blocks are not
possible
In static inner class main method, static blocks are possible
Outer.inner I = new Outer(). New Inner(); //// normal Inner object
creation
Outer.Inner I = new Outer().Inner(); // static Inner object
creation
Ex - 1
public class StatiNestedClasses {
static int i=10;
static int j=20;
static class Inner{
int a=30;
static int b=40;
void dis() {
System.out.println(a+b);
System.out.println(i+j);
}
}
public static void main(String[] args) {
//new StatiNestedClasses().new
Inner().dis();
new StatiNestedClasses.Inner().dis();
}
}
Note :-
Ex 1:
class TestCLient
{ public static void main (String[] args)
{ Test t = new Test();
t.disp();
}
}
class TestClient
{ A a = new A ()
{ void disp() { System.out.println("Good Evening");
System.out.println(a.getClass().getName()); }
};
public static void main(String[] args) {
TestClient t = new TestClient();
t.a.disp();
}
}
Output :-
Good Evening
TestClient$l