Adavantage of inner class--
- one time use
- supports and improves encapsulation
- readibility
- private field access
Without existing of outer class inner class will not exist.
class car{ class wheel{ }}
There are four types of inner class.
- normal inner class
- Method Local Inner class
- Anonymous inner class
- static inner class
point ---
- from static inner class ,we can only access static member of outer class.
- Inside inner class we cananot declare static member .
inorder to invoke normal inner class in static area of outer class.
Outer 0=new Outer();Outer.Inner i= O.new Inner();
inorder to invoke normal inner class in instance area of outer class.
Inner i=new Inner();
inorder to invoke normal inner class in outside of outer class.
Outer 0=new Outer();Outer.Inner i= O.new Inner();
inside Inner class This pointer to inner class.
this.member-current inner classouterclassname.this--outer class
for inner class applicable modifier is -- public,default,
final,abstract,strictfp,+private,protected,static
outer$inner is the name of inner class name.
inner class inside instance method then we can acess static and instance field of outer class.
10.inner class inside static method then we can access only static field of
outer class.
class outer{ int x=10; static int y-20; public void m1() { int i=30; final j=40; class inner{ public void m2() { // have accees x,y and j } } }}