Saturday, February 7, 2009

inner class,Method local inner class,Anonymous inner class,static inner class

If we can keep a class inside another class such type of classes are called inner class.Sun people has introduced inner classes concept in 1.1 version for implementing event handling.But because of powerful feature of inner classes ,now a days we are using in general programming also,just like normal concept.
Based on the position of declaration and purpose / behavior of inner classes, all the inner classes are divided into 4 types.
1.Normal or Regular inner class
2.Method local inner class.
3.Anonymous inner class.
4.static inner class.

1. Normal or Regular inner class:
If we are keeping a named class directly with in a another class , such classes are called Normal or Regular inner classes.
inner class Example:
class Outer
{
class Inner
{
}
public static void main(String ar[])
{
System.out.println(“java”);
}
}
outer . class ,outer$inner.class are created by compiler

  • If we run as java Outer output we will get is java
  • If we run as java outer$inner output: RTE no such method error : main
  • Inside inner class we are not allowed to keep any static declaration .violation leads to compile time error.
  • As the static declaration are not allowed in side normal inner class ,we can’t run inner class directly from the console .
  • We can access inner class code through outer class object only .

Inside inner class ,we are allowed to access all the members of outer class (both static and non-static)
Inner class code:
class Outer
{
private static int x=10;
private int y=10;
class Inner
{
public void m1()
{
System.out.println(“static variable”+”…”+x);
System.out.println(“instance variable ”+”…”+y);
}
}
public static void main(String ar[])
{
Outer o=new Outer();
Outer.Inner in=o.new Inner(); //Outer.Inner in=new Outer().new Inner();
in.m1();
}
}

creating inner class object:
with in the static method of outer class or outside outer class.
Outer o=new Outer()
Outer.Inner in=o.new Inner();

Or

Outer.Inner in=new Outer().new Inner();
With in the instance area of outer class
Inner in=new Inner();
Inner class object never exist stand alone with out having outer class object.
From the normal regular inner class we can access any member (either static or non static ) of outer class.
To access static and instance variables of outer class from inner class:
Example code:
class Outer
{
int x=10;
static int y=20;
class Inner
{
int i=30;
public void m1()
{
System.out.println(x+”….”+y);
}
}
public static void main(String ar[])
{
Outer o=new Outer();
Outer.Inner in=o.new Inner();
in.m1(); // 10 …20
System.out.println(in.x+”….”+in.y); // CTE (here x and y are not instance variables of inner class)
System.out.println(in.l); // 30
o.m2();
}
}
All the members of outer class (either static or non-static ) can be accessed from inner class .But on instance of inner class.We are not allowed to invoke members of outer class directly.
inner class with this keyword Example:
class Outer
{
int x=10;
class Inner
{
int x=50;
public void m1()
{
System.out.println(x); // 50
System.out.println(this.x); // 50
System.out.println(Outer.this.x); // 10
}
}
public static void main(String ar[])
{
Outer o=new Outer();
Outer.Inner in=o.new Inner(); //Outer.Inner in=new Outer().new Inner();
in.m1();
}
}
  • Inside inner class ,this always refers to current inner class reference only.
  • To refer current outer class object we have to use <outer class name> .this
  • For normal regular Inner classes ,the following are the allowed modifiers.
  • public ,default,private,protected,abstract,static,strictfp and final
Method local inner class :
  • If we are writing an inner class inside a method (static or non-static),such type of inner classes are called Method local inner clases.
  • These inner class are very rarely used classes in real time.Because ,these inner classes can be accessed with in the method only.Outside the method ,your not allowed to access ,violation leads to CTE.
Method local inner class Example:
class Outer
{
// int x=10;
public void m1()
{
int y=20;
class Inner
{
public void display(int x,int y)
{
System.out.println(“SUM IS”+(x+y));
}
}
Inner in=new Inner();
in.display(10,20); //30
in.display(100,200); //300
}
}
public static void main(String ar[])
{
Outer o=new Outer()
o.m1();
}
}
  • we can access method local inner class with in the method ,where it is declared .But from outside the method ,we are not allowed to access that inner class code.
  • The only allowed modifier for the method local inner classes are final and abstract but not both simultaneously.So,we are not allowed to declare a method local inner class as public ,protectedm,static,….
Example code:
class Outer

{
int x=10;
public void m1()
{
int y=20;
class Inner
{
public void show()
{
System.out.println(x); // 10
System.out.println(y); // Compile time error ( If y is final no error)
}
}
Inner in=new Inner();
in.show();
}
public static void main(String ar[])
{
Outer o=new Outer()
o.m1();
}
}
  • From method local inner class we are allowed to access instance variables of outer class but we are allowed to access local variables of that method,which this inner class declared.
  • If we are declaring the local variable as the final ,then we are allowed to access local variable from method local inner class.
  • If we are declaring an inner class ,with in a static method then only static members of outer class ,we are allowed to access from that inner class.
Anonymous inner class:
We are allowed to declare inner classes with out name ,such type of inner classes are known as anonymous inner classes.
  • Anonymous inner class that extends a class
  • Anonymous inner class that implements an interface
  • Anonymous inner class declared in method arguments.
1.Anonymous inner class that extends a class:
Anonymous inner class Example code:
class Popcorn
{
public void eat()
{
System.out.println(“so sweet”);
}
class Sample
{
public static void main(String arg[])
{
Popcorn p=new Popcorn() // no semicolon (;) here
{
public void eat()
{
System.out.println(“so spicy”);
}
}; // with out semicolon (;) leads compile time error
p.eat(); // so spicy
}
}
In the Anonymous inner classes,we are allowed to define new methods but we are not allowed to access those new methods by using parent class reference .these methods just for internal use only.
Example code:
class Sample
{
public static void main(String arg[])
{
Thread t=new Thread()
{
public void run()
{
System.out.println(“child thread job”);
}
};
t.start();
System.out.println(“main thread job”);
}
}

2. Anonymous inner class that implements an interface :
with out Anonymous inner class code using thread :
class MyRunnable implements Runnable
{
public void run()
{
System.out.println(“run”);
}
}
class Test
{
public static void main(String ar[])
{
MyRunnable m=new MyRunnable();
Thread t=new Thread(m);
t.start();
}
}
with Anonymous inner class using thread:
class Test
{
public static void main(String arg[])
{
Runable r=new Runnable()
{
public void run()
{
System.out.println(“run”);
}
};
Thread t=new Thread(r);
t.start();
}
}
  • A class can extends only one class at a time ,of course Anonymous inner class also can extend only one class.
  • A class can implements any number of interfaces but anonymous inner class can can implement only one interface at a time.
  • A class can extend a class and implement an interface at atime ,but anonymous inner class can extend a class or implement an interface ,but not both at a time.
3.Anonymous inner class declared in method arguments:
Anonymous inner class example:
class Sample
{
public static void main(String arg[])
{
new Thread( new Runnable()
{
public void run()
{
System.out.println(“run”);
}
});.start();
or
If you write like this in above Thread t= new Thread( new Runnable(){……}
t.start();

  • We can declare anonymous inner class either in static or non static methods .
  • If we declare anonymous inner class in the static method the static variables of outer class can be accessed directly from inner class.
  • If we are declaring anonymous inner class inside instance method ,we can access both instance and static variables of outer class directly from the inner class.
  • From anonymous inner class we are not allowed to access the local variables of the method unless it is marked as final.

4.Static nested Inner class:
  • If we are declaring a classs inside a class with the keyword static such type of classes are called nested classes.
  • The objects of static nested class exists stand alone,with out associateing the outer class object.
Static nested class example:
class Outer

{
static class Inner
{
public void m1()
{
System.out.println(“hai”);
}
}
public static void main(String art[])
{
Outer.Inner in =new Outer.Inner();
in.m1(); // hai
}
}

If method is static public void m1() { System.out.println(“hai”); } we can call that method with out create object Outer.Inner.m1();
Static nested inner class Example:
class Outer
{
static class Inner
{
public void m1()
{
System.out.println(“hai”);
}
public static void main(String ar[])
{
System.out.println(“hello”);
}
}
public static void main(String ar[])
{
Outer.Inner.m1();
}
}
compile : javac Outer.java
run: java Outer$.1 or java Outer$Inner.

  • Static nested class objects never associated with outer class object .
  • Inside static nested classes we can keep both static and non-static declarations.
  • We can run directly the static nested class from the command prompt as we are allowed to keep main () method inside that class.

0 comments:

Post a Comment