Monday, August 6, 2007

Inner classes

Hi everyone!!!
I really mised out the weekend!It was like i had not attended the class from a month.Neways, Mangesh helped me a lot by letting me know what had been taught on Friday.Thanx Mangesh!!!
Here, i m irting about inner classes.Hope it ould help u ppl in ome way!!

Inner classes are a feature added to Java with the release of JDK 1.1. Inner classes, sometimes also called as Nested classes, can give your programs additional carity and make them more concise.

Basically, an inner class is same as any other class except that it is declared inside an opening and closing braces of some construct that may be a class or a method.Following is an example of inner classes:

public class OuterOne{

private int x;

public class InnerOne{
private int y;
public innerMethod(){
System.out.println("Y = " + y);
}
}

public void outerMethod(){

System.out.println(" X = " + x);
}

}


When an inner class is declared like this, the enclosing class name becomes part of the fully qualified name of the inner class.In this case, the full names of the two classes are OuterOne and OuterOne.Innerone. itis possible since an inner class belongs to its enclosing class similar to the way a class belongs to a package.

Following example shows an extended view at the inner classes where the inner class uses the members of the enclosing class.

public class OuterOne{

private int x;

public class InnerOne{
private int y;
public innerMethod(){
System.out.println("Enclosing X = " + y);
System.out.println("Y = " + y);
}
}

public void outerMethod(){

System.out.println(" X = " + x);
}

}


Sometimes you might to create an instance of an inner class from a static method, or in some situation where ther is no this object available. The situtaion arises in a main() method.You can ahieve this by using new operator as :

public sytatic void main(String[] args){

OuterOne.InnerOne inner = new OuterOne().InnerOne();
inner.innerMethod();
}


We can also define classes inside Methods.The first point to be noted here is that anything declared inside a method is not a member of the class, but is local to the method.The imediate consequence is that classes declared in methods are private to the method and cannot be marked with any access modifier, neither can they be marked as static.

The second and most important point to be noted here is that the classes defined inside a method can access variables of the enclosing methods if and only if those variables are defined as FINAL.
The reason is that: An onject declared inside a mehod is likely to outlive the method invocation.Since the local variables and method arguments are conventionally destroyed when their method exits,these variables will be invalid for access by inner class methods after the enclosing method exits. By allowing the methods to access only final variables, it is possible to maintain a copy of the variables with the object itself, therby extending their lifetime.


Anonymous classes: Some classes that you define inside a method do not need a name , such classes are called as Anonymous classes.

Anonymous classes are mainly used to extend some class or implement an interface.This restricts you to extend more than one class and implement more than one interface.

Since there is no name given to the anonymous classes, we cannot use the new keyword in the usual way.The definition, contsruction and instantiation of an anonymous class all occur in the same place.This is explained in the next example:

public void aMethod(){
theButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.pritnln("The action has occurred");
}
}
);
}

This example may look somewhat weird to you since we have not learnt ActionListener yet.So lets check this example for our IStatusPrinter interface where we will use the Flexigame object to set the IStatusPrinter reference:

Flexigame fgame = new Flexigame("Cricket");

fgame.setStatusPrinter(
new IStatusPrinter(){
public void start(){
System.out.println("Starting the Game thgrough Anonymous class");
}
public void stop(){
System.out.println("Stopping the Game thgrough Anonymous class");
}

});


Thats all for now, guys!!!
See u soon with more stuff!!!
Take care,
Regards,
S A Vakeel

No comments: