*sun microsystem release the inner classes in java 1.1 version. a class within another class is called inner class. it is possible to apply the public ,default ,abstract, final ,strictfp,private,protected and static modifier on inner class. * whenever we apply public on inner class ,it is possible to access outside. * whenever we apply protected on inner class, it is only possiblet ot access inside a package.It is not possible to access outside of the package.It is critical to creating the subclass to inner class at outside of the package. * default inner class work only that package. * private innner class work only that outer class. * abstract innner class has contatin abstract methods.But the abstract methods are implementes only within another subclass of same class. * it is not possible to create sub class to final inner class.But final and abstract are illegal combination. * whenever we declare the strictfp on inner class, that inner class follows IEEE 754 standart for floating point calculations. 1)regular inner class: This regular inner class does not contatin static members.But it is possible to access the static members and instance members of the outer class. ex: class Outer { int a=10; static int b=20; class Inner { int a=30; /* static String name="basha"; compile time error(ce): because regular inner class does not contain static members */ public void show() { int a=40; System.out.println(a); System.out.println(this.a); System.out.println(Outer.this.a); System.out.println(b); } }//Inner public static void main(String args[]) { Outer o=new Outer(); Outer.Inner i=o.new Inner(); i.show(); } }//Outer
Q) what happen whenever we compile the inner class ?
whenever we compile the inner class, the compiler generate the inner class like class Outer$Inner extends java.lang.Object { final Outer this$0; Outer$Inner(Outer o) { this$0=o; } public void show(); } 2)method local inner class : * declaring the inner class inside the method is called the method inner class. * the method inner class can access the static and instance variables of outer class. * the method inner class access the only final local variables of the method. * the method inner class does not contain any static declarations. * the scope of the method inner class is only that method ,Hence we create the object inside the method after declaring the method inner class. ex: class Outer { int a=10; static int b=20; public void show() { final int c=30; int d=40; class MInner { static int e=50 ;// ce: method local inner class does not contain static members. public void iShow() { System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); //ce: because d is not a constant. } }//MInner MInner mi=new MInner(); mi.ishow(); }//show() public static void main(String args[]) { Outer o=new Outer(); o.show(); } }//Outer 3)annoymous inner class: if a inner class does not have any name than that inner class is called annoymous inner class. the annoymous inner class is sub class to parent class but it does not have any name. hear annoymous reference values is handled by the super class reference variable. a)declared by extending the a class class Parent { } class Demo { public static void main(String args[]) { //this code is creating the annoymous class that is sub class to Parent class and handling the // reference value by Parent class reference variable Parent p=new Parent() { void show() { System.out.println("annoymous inner classs show()"); } } } } b) declared by implementing interface: class Demo { public static void main(String args[]) { Runnable r=new Runnable() { public void run() { System.out.println("run method is called"); } } Thread t=new Thread(r); t.start(); }//main }//Demo c)declared as method argument: class Demo { public static void main(String args[]) { Thread t=new Thread(new Runnable() { public void run() { System.out.println("run method is executed"); } }); t.start(); } } 4)static inner class: * declaring the inner class by using static modifier. * the static inner class has contain static members and also instance member. * but static inner class does not access the instatnce variables of outer class. * this static inner class not really inner class ,because it is executed without depends on outer class. class Outer { int a=2; static int b=3; static class Inner { public static void main(String args[]) { //System.out.println(a);//ce System.out.println(b); } }//Inner }//Outer compile: javac Outer.java run : java Outer$Inner
where we use:
Generally we are using only annonymaus innerclass only as method argument in swings and threads and spring framework
Ads