Java 8 - Anonymous Inner class with Lambda Expression:

Anonymous Inner class with Lambda Expression:

Where we are using Anonymous inner class, there may be chance of using Lambda expression.

What is Anonymous Inner Class:
Class without having any name that is called Anonymous inner class,

Where we will use:

Eg Java7:

Class ThreadDemo{
public static void main (String args[]){
Runnable r = new Runnable(){
public void run(){
for(int i=0;i<10;i++){
System.out.println("Child Thread");
}
}
};
Thread t = new Thread(r);
t.start();
for(int i=0;i<10;i++){
System.out.println("Main Thread");
}
}
}

Java 8:
Class ThreadDemo{
public static void main (String args[]){
Runnable r = () -> {
for(int i=0;i<10;i++){
System.out.println("Child Thread");
}
};
Thread t = new Thread(r);
t.start();
for(int i=0;i<10;i++){
System.out.println("Main Thread");

}
}


Note : Every Anonymous Class can we replace with Lambda Expression ? No. Because of below rules

important:
* Anonymous innerclass is not equal to Lambda Expression.
* If Anonymous innerclass implements interface which contain only one method, then we can go for Lambda expression.

Rules:
1. If there is no interface concept then you cannot go for Lambda.
2. Anonymous innerclass can extend concrete class, But Lambda cannot extend concrete class.
3. Anonymous innerclass can extend abstract class, But Lambda cannot extend abstract class.
3. Anonymous innerclass can extend multiple methods in the interface, But Lambda cannot extend multiple methods in the interface.
Below are the examples,

Case 1: Anonymous Inner class extends Concrete Class
Class Test{

}

Test t = new Test{

}

Case 2: Anonymous Inner class extends abstract class
abstract Class Test{

}

Test t = new Test{

}

Case 3: Anonymous Inner class implements interface (With multiple abstract methods)

interface Test{
public void m1();
public void m2();
public void m3();
}

Test t = new Test{
public void m1(){
}
public void m2(){
}
public void m3(){
}

}

Case 4: Anonymous Inner class implements interface (With Single abstract methods)

interface Test{
public void m1();
}

Test t = new Test{
public void m1(){
}
}

Result:
In Above 4 cases only 4th case alone we can go for Lambda Because of above rules.


this Keyword:
* Can we declare instance variable inside the Anonymous inner Class ? Yes
* if you use this.x (x is a variable which present inside the anonymous inner class and inside normal class). It will refer the varible which is present iside the anonymous inner class only.
* this will refer current inner class instance.

Eg.
interface Interf{
public void m1();
}

Java 7:

Class Test{
int x = 888;  // Instance variable
public void m2(){
Interf i = new Interf(){
int x=999;    // Instance Variable of inner class
public void m1(){
System.out.println(this.x);  // 999
System.out.println(Test.this.x); // 888
}
};
i.m1();
public static void main(String[] args){
Test t = new Test();
t.m2();
}
}
}

Java 8:
* We cannot create instance variable inside the Lambda Expression.

Class Test{
int x = 888;
public void m2(){
Interf i = () -> {
int x=999;   // Local Variable
System.out.println(this.x);  // 888
System.out.println(x);  // 999
};
i.m1();
}
public static void main(String args[]){
Test t = new Test();
t.m2();
}
}

Variables:
Note:
* No problem to access different instance variable and different local variable inside the Lambda.
* But the local variable always treat as a final variable. If you declared or not. Inside the lambda expression local variables are treating as a final variable.
Eg.
Class Test{
int x = 888; // Instance variable
public void m2(){
Interf i = () -> {
int y=999;   // Local Variable
System.out.println(x);  // 888
System.out.println(y);  // 999
x=889;  // True statement
y=1000;  // False statement. Because internally it is treating as a final variable , Compile Error

};
i.m1();
}
public static void main(String args[]){
Test t = new Test();
t.m2();
}
}

Comments

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab