Monday, November 23, 2009

All about Java Inner class with sample.

package blog.java;

class OuterClass {

private String aVariable = "Normal private variable.";

private static String staticVar = "private static variable.";

// Normal Inner class example.
class InnerClass {
public void aMethod() {
System.out.println("aMethod of InnerClass called.");
// Any Variable of the outer class is accessable from inner class.
System.out.println(" Accessing aVariable: " + aVariable);
System.out.println();

}
}

// Static Inner class example.
static class staticInnerClass {

void aMethod() {
System.out.println("aMethod of staticInnerClass called.");
// Only Static Variable of the outer class is accessable from inner
// class.
System.out.println(" Accessing a static Variable:" + staticVar);
System.out.println();
}
}

// Method-Local Inner Class example.
public void aMethod() {
final String msg = "Method-Local Inner Classes";
System.out.println("aMethod from the OuterClass called.");

// Method-Local Inner Class
class MethodLocalInnerClass {

String variable = "MethodLocalInnerClass variable.";

void aMethod() {
System.out
.println("aMethod from the MethodLocalInnerClass called.");
// Can access final local variable declared in the method.
System.out
.println(" Accessing a local Variable in the method: "
+ msg);
System.out.println();
}

}

// initialization of Method-Local Inner Class
MethodLocalInnerClass methodLocalInnerClass = new MethodLocalInnerClass();
System.out.println("variable from the MethodLocalInnerClass: "
+ methodLocalInnerClass.variable);
methodLocalInnerClass.aMethod();

}

public void initInnerClass() {
// Normal invocation of InnerClass works within the outer class
InnerClass innerClass = new InnerClass();
innerClass.aMethod();
}
}

class DemoClass {
public void aMethod() {
System.out.println("aMethod called from demoClass");
}

}

interface DemoInterface {
void aMethod();
}

class MethodArgInnerClass {
public void anotherMethod(DemoInterface demoInterface) {
System.out.println("anotherMethod of methodArgInnerClass called.");
demoInterface.aMethod();
}
}

public class InnerClassSample {

public static void main(String[] args) {
// Get a reference to outer class.
OuterClass outerClass = new OuterClass();

// initialization of Normal Inner class
OuterClass.InnerClass innerClass = outerClass.new InnerClass();
innerClass.aMethod();

// initialization of Static Inner class
OuterClass.staticInnerClass innerClass2 = new OuterClass.staticInnerClass();
innerClass2.aMethod();

// Demo of Method-Local Inner Class
outerClass.aMethod();

// Following are the demo of Anonymous Inner class in action.
/**
* Example of overriding the method: This anonymous class now
* becomes the subclass of the DemoClass.java
*/
DemoClass demoClass = new DemoClass() {

public void aMethod() {
System.out.println("aMethod over ridden from "
+ "Anonymous Inner class of demoClass");
}
};

demoClass.aMethod();

// Example of implementing a Interface
DemoInterface interface1 = new DemoInterface() {

public void aMethod() {
System.out.println("aMethod method of the DemoInterface "
+ "implemented by Anonymous Inner class");

}

};

interface1.aMethod();

// Example of Anonymous Inner class in method argument.
MethodArgInnerClass innerClass3 = new MethodArgInnerClass();
innerClass3.anotherMethod(new DemoInterface() {

public void aMethod() {
System.out.println("Example of Anonymous Inner class"
+ " in method argument");
}
});

}

}